Changeset 36784 in webkit for trunk/JavaScriptCore/API


Ignore:
Timestamp:
Sep 22, 2008, 4:01:43 PM (17 years ago)
Author:
[email protected]
Message:

2008-09-22 Kelvin Sherlock <[email protected]>

Updated and tweaked by Sam Weinig.

Reviewed by Geoffrey Garen.

Bug 20020: Proposed enhancement to JavaScriptCore API
<https://bugs.webkit.org/show_bug.cgi?id=20020>

Add JSObjectMakeArray, JSObjectMakeDate, JSObjectMakeError, and JSObjectMakeRegExp
functions to create JavaScript Array, Date, Error, and RegExp objects, respectively.

  • API/JSObjectRef.cpp: The functions
  • API/JSObjectRef.h: Function prototype and documentation
  • JavaScriptCore.exp: Added functions to exported function list
  • API/tests/testapi.c: Added basic functionality tests.
  • kjs/DateConstructor.cpp: Replaced static JSObject* constructDate(ExecState* exec, JSObject*, const ArgList& args) with JSObject* constructDate(ExecState* exec, const ArgList& args). Added static JSObject* constructWithDateConstructor(ExecState* exec, JSObject*, const ArgList& args) function
  • kjs/DateConstructor.h: added prototype for JSObject* constructDate(ExecState* exec, const ArgList& args)
  • kjs/ErrorConstructor.cpp: removed static qualifier from ErrorInstance* constructError(ExecState* exec, const ArgList& args)
  • kjs/ErrorConstructor.h: added prototype for ErrorInstance* constructError(ExecState* exec, const ArgList& args)
  • kjs/RegExpConstructor.cpp: removed static qualifier from JSObject* constructRegExp(ExecState* exec, const ArgList& args)
  • kjs/RegExpConstructor.h: added prototype for JSObject* constructRegExp(ExecState* exec, const ArgList& args)
Location:
trunk/JavaScriptCore/API
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/JSObjectRef.cpp

    r36726 r36784  
    11/*
    22 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
     3 * Copyright (C) 2008 Kelvin W Sherlock ([email protected])
    34 *
    45 * Redistribution and use in source and binary forms, with or without
     
    2829
    2930#include "APICast.h"
     31#include "DateConstructor.h"
     32#include "ErrorConstructor.h"
    3033#include "FunctionConstructor.h"
     34#include "JSArray.h"
    3135#include "JSCallbackConstructor.h"
    3236#include "JSCallbackFunction.h"
     
    4145#include "ObjectPrototype.h"
    4246#include "PropertyNameArray.h"
     47#include "RegExpConstructor.h"
    4348#include "identifier.h"
    4449#include <wtf/Platform.h>
     
    128133        result = 0;
    129134    }
     135    return toRef(result);
     136}
     137
     138JSObjectRef JSObjectMakeArray(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[],  JSValueRef* exception)
     139{
     140    ExecState* exec = toJS(ctx);
     141    exec->globalData().heap->registerThread();
     142    JSLock lock(exec);
     143
     144    JSObject* result;
     145    if (argumentCount) {
     146        ArgList argList;
     147        for (size_t i = 0; i < argumentCount; ++i)
     148            argList.append(toJS(arguments[i]));
     149
     150        result = constructArray(exec, argList);
     151    } else
     152        result = constructEmptyArray(exec);
     153
     154    if (exec->hadException()) {
     155        if (exception)
     156            *exception = toRef(exec->exception());
     157        exec->clearException();
     158        result = 0;
     159    }
     160
     161    return toRef(result);
     162}
     163
     164JSObjectRef JSObjectMakeDate(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[],  JSValueRef* exception)
     165{
     166    ExecState* exec = toJS(ctx);
     167    exec->globalData().heap->registerThread();
     168    JSLock lock(exec);
     169
     170    ArgList argList;
     171    for (size_t i = 0; i < argumentCount; ++i)
     172        argList.append(toJS(arguments[i]));
     173
     174    JSObject* result = constructDate(exec, argList);
     175    if (exec->hadException()) {
     176        if (exception)
     177            *exception = toRef(exec->exception());
     178        exec->clearException();
     179        result = 0;
     180    }
     181
     182    return toRef(result);
     183}
     184
     185JSObjectRef JSObjectMakeError(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[],  JSValueRef* exception)
     186{
     187    ExecState* exec = toJS(ctx);
     188    exec->globalData().heap->registerThread();
     189    JSLock lock(exec);
     190
     191    ArgList argList;
     192    for (size_t i = 0; i < argumentCount; ++i)
     193        argList.append(toJS(arguments[i]));
     194
     195    JSObject* result = constructError(exec, argList);
     196    if (exec->hadException()) {
     197        if (exception)
     198            *exception = toRef(exec->exception());
     199        exec->clearException();
     200        result = 0;
     201    }
     202
     203    return toRef(result);
     204}
     205
     206JSObjectRef JSObjectMakeRegExp(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[],  JSValueRef* exception)
     207{
     208    ExecState* exec = toJS(ctx);
     209    exec->globalData().heap->registerThread();
     210    JSLock lock(exec);
     211
     212    ArgList argList;
     213    for (size_t i = 0; i < argumentCount; ++i)
     214        argList.append(toJS(arguments[i]));
     215
     216    JSObject* result = constructRegExp(exec, argList);
     217    if (exec->hadException()) {
     218        if (exception)
     219            *exception = toRef(exec->exception());
     220        exec->clearException();
     221        result = 0;
     222    }
     223   
    130224    return toRef(result);
    131225}
  • trunk/JavaScriptCore/API/JSObjectRef.h

    r34606 r36784  
    11/*
    22 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
     3 * Copyright (C) 2008 Kelvin W Sherlock ([email protected])
    34 *
    45 * Redistribution and use in source and binary forms, with or without
     
    430431
    431432/*!
     433 @function
     434 @abstract Creates a JavaScript Array object.
     435 @param ctx The execution context to use.
     436 @param argumentCount An integer count of the number of arguments in arguments.
     437 @param arguments A JSValue array of data to populate the Array with. Pass NULL if argumentCount is 0.
     438 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
     439 @result A JSObject that is an Array.
     440 @discussion The behavior of this function does not exactly match the behavior of the built-in Array constructor. Specifically, if one argument
     441 is supplied, this function returns an array with one element.
     442 */
     443JS_EXPORT JSObjectRef JSObjectMakeArray(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
     444
     445/*!
     446 @function
     447 @abstract Creates a JavaScript Date object, as if by invoking the built-in Date constructor.
     448 @param ctx The execution context to use.
     449 @param argumentCount An integer count of the number of arguments in arguments.
     450 @param arguments A JSValue array of arguments to pass to the Date Constructor. Pass NULL if argumentCount is 0.
     451 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
     452 @result A JSObject that is a Date.
     453 */
     454JS_EXPORT JSObjectRef JSObjectMakeDate(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
     455
     456/*!
     457 @function
     458 @abstract Creates a JavaScript Error object, as if by invoking the built-in Error constructor.
     459 @param ctx The execution context to use.
     460 @param argumentCount An integer count of the number of arguments in arguments.
     461 @param arguments A JSValue array of arguments to pass to the Error Constructor. Pass NULL if argumentCount is 0.
     462 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
     463 @result A JSObject that is a Error.
     464 */
     465JS_EXPORT JSObjectRef JSObjectMakeError(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
     466
     467/*!
     468 @function
     469 @abstract Creates a JavaScript RegExp object, as if by invoking the built-in RegExp constructor.
     470 @param ctx The execution context to use.
     471 @param argumentCount An integer count of the number of arguments in arguments.
     472 @param arguments A JSValue array of arguments to pass to the RegExp Constructor. Pass NULL if argumentCount is 0.
     473 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
     474 @result A JSObject that is a RegExp.
     475 */
     476JS_EXPORT JSObjectRef JSObjectMakeRegExp(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
     477
     478/*!
    432479@function
    433480@abstract Creates a function with a given script as its body.
  • trunk/JavaScriptCore/API/tests/testapi.c

    r35900 r36784  
    877877    ASSERT(count == 1); // jsCFString should not be enumerated
    878878
     879    JSValueRef argumentsArrayValues[] = { JSValueMakeNumber(context, 10), JSValueMakeNumber(context, 20) };
     880    o = JSObjectMakeArray(context, sizeof(argumentsArrayValues) / sizeof(JSValueRef), argumentsArrayValues, NULL);
     881    string = JSStringCreateWithUTF8CString("length");
     882    v = JSObjectGetProperty(context, o, string, NULL);
     883    assertEqualsAsNumber(v, 2);
     884    v = JSObjectGetPropertyAtIndex(context, o, 0, NULL);
     885    assertEqualsAsNumber(v, 10);
     886    v = JSObjectGetPropertyAtIndex(context, o, 1, NULL);
     887    assertEqualsAsNumber(v, 20);
     888
     889    o = JSObjectMakeArray(context, 0, NULL, NULL);
     890    v = JSObjectGetProperty(context, o, string, NULL);
     891    assertEqualsAsNumber(v, 0);
     892    JSStringRelease(string);
     893
     894    JSValueRef argumentsDateValues[] = { JSValueMakeNumber(context, 0) };
     895    o = JSObjectMakeDate(context, 1, argumentsDateValues, NULL);
     896    assertEqualsAsUTF8String(o, "Wed Dec 31 1969 16:00:00 GMT-0800 (PST)");
     897
     898    string = JSStringCreateWithUTF8CString("an error message");
     899    JSValueRef argumentsErrorValues[] = { JSValueMakeString(context, string) };
     900    o = JSObjectMakeError(context, 1, argumentsErrorValues, NULL);
     901    assertEqualsAsUTF8String(o, "Error: an error message");
     902    JSStringRelease(string);
     903
     904    string = JSStringCreateWithUTF8CString("foo");
     905    JSStringRef string2 = JSStringCreateWithUTF8CString("gi");
     906    JSValueRef argumentsRegExpValues[] = { JSValueMakeString(context, string), JSValueMakeString(context, string2) };
     907    o = JSObjectMakeRegExp(context, 2, argumentsRegExpValues, NULL);
     908    assertEqualsAsUTF8String(o, "/foo/gi");
     909    JSStringRelease(string);
     910    JSStringRelease(string2);
     911
    879912    JSClassDefinition nullDefinition = kJSClassDefinitionEmpty;
    880913    nullDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
Note: See TracChangeset for help on using the changeset viewer.