Changeset 15463 in webkit for trunk/JavaScriptCore


Ignore:
Timestamp:
Jul 15, 2006, 6:40:07 PM (19 years ago)
Author:
ggaren
Message:

Reviewed by Maciej.


  • JSObjectMakeFunctionWithBody includes a function name and named parameters now.
  • API/JSObjectRef.cpp: (JSObjectMakeFunctionWithBody):
  • API/JSObjectRef.h:
  • API/testapi.c: (assertEqualsAsUTF8String): More informative failure reporting. (main): Test more function cases.
Location:
trunk/JavaScriptCore
Files:
4 edited

Legend:

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

    r15462 r15463  
    9090}
    9191
    92 JSObjectRef JSObjectMakeFunctionWithBody(JSContextRef context, JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
    93 {
    94     JSLock lock;
    95    
    96     ExecState* exec = toJS(context);
     92JSObjectRef JSObjectMakeFunctionWithBody(JSContextRef context, JSStringRef name, unsigned parameterCount, JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
     93{
     94    JSLock lock;
     95   
     96    ExecState* exec = toJS(context);
     97    UString::Rep* nameRep = name ? toJS(name) : &UString::Rep::null;
    9798    UString::Rep* bodyRep = toJS(body);
    98     UString jsSourceURL = UString(toJS(sourceURL));
    99 
    100     if (!bodyRep)
    101         bodyRep = &UString::Rep::null;
    102    
    103     int sid;
    104     int errLine;
    105     UString errMsg;
    106     RefPtr<FunctionBodyNode> bodyNode = Parser::parse(jsSourceURL, startingLineNumber, bodyRep->data(), bodyRep->size(), &sid, &errLine, &errMsg);
    107     if (!bodyNode) {
    108         if (exception)
    109             *exception = toRef(Error::create(exec, SyntaxError, errMsg, errLine, sid, jsSourceURL));
    110         return 0;
    111     }
    112 
    113     ScopeChain scopeChain;
    114     scopeChain.push(exec->dynamicInterpreter()->globalObject());
    115     return toRef(static_cast<JSObject*>(new DeclaredFunctionImp(exec, "anonymous", bodyNode.get(), scopeChain)));
     99    UString::Rep* sourceURLRep = sourceURL ? toJS(sourceURL) : &UString::Rep::null;
     100   
     101    Identifier nameIdentifier = nameRep ? Identifier(nameRep) : Identifier("anonymous");
     102   
     103    List args;
     104    for (unsigned i = 0; i < parameterCount; i++)
     105        args.append(jsString(UString(toJS(parameterNames[i]))));
     106    args.append(jsString(UString(bodyRep)));
     107
     108    JSObject* result = exec->dynamicInterpreter()->builtinFunction()->construct(exec, args, nameIdentifier, UString(sourceURLRep), startingLineNumber);
     109    if (exec->hadException()) {
     110        if (exception)
     111            *exception = toRef(exec->exception());
     112        exec->clearException();
     113        result = 0;
     114    }
     115    return toRef(result);
    116116}
    117117
  • trunk/JavaScriptCore/API/JSObjectRef.h

    r15462 r15463  
    398398@abstract Creates a function with a given script as its body.
    399399@param context The execution context to use.
     400@param A JSString containting the function's name. Pass NULL to create an anonymous function.
     401@param parameterCount An integer count of the number of parameter names in parameterNames.
     402@param parameterNames A JSString array containing the names of the function's parameters. Pass NULL if parameterCount is 0.
    400403@param body A JSString containing the script to use as the function's body.
    401404@param sourceURL A JSString containing a URL for the script's source file. This is only used when reporting exceptions. Pass NULL if you do not care to include source file information in exceptions.
    402405@param startingLineNumber An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions.
    403406@param exception A pointer to a JSValueRef in which to store a syntax error exception, if any. Pass NULL if you do not care to store a syntax error exception.
    404 @result A JSObject that is an anonymous function, or NULL if body contains a syntax error. The returned object's prototype will be the default function prototype.
     407@result A JSObject that is a function, or NULL if either body or parameterNames contains a syntax error. The object's prototype will be the default function prototype.
    405408@discussion Use this method when you want to execute a script repeatedly, to avoid the cost of re-parsing the script before each execution.
    406409*/
    407 JSObjectRef JSObjectMakeFunctionWithBody(JSContextRef context, JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception);
     410JSObjectRef JSObjectMakeFunctionWithBody(JSContextRef context, JSStringRef name, unsigned parameterCount, JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception);
    408411
    409412/*!
     
    521524@param thisObject The object to use as "this," or NULL to use the global object as "this."
    522525@param argumentCount An integer count of the number of arguments in arguments.
    523 @param arguments A JSValue array of the  arguments to pass to the function.
     526@param arguments A JSValue array of the  arguments to pass to the function. Pass NULL if argumentCount is 0.
    524527@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.
    525528@result The JSValue that results from calling object as a function, or NULL if an exception is thrown or object is not a function.
     
    539542@param object The JSObject to call as a constructor.
    540543@param argumentCount An integer count of the number of arguments in arguments.
    541 @param arguments A JSValue array of the  arguments to pass to the function.
     544@param arguments A JSValue array of the  arguments to pass to the function. Pass NULL if argumentCount is 0.
    542545@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.
    543546@result The JSObject that results from calling object as a constructor, or NULL if an exception is thrown or object is not a constructor.
  • trunk/JavaScriptCore/API/testapi.c

    r15462 r15463  
    5858    JSStringGetUTF8CString(valueAsString, jsBuffer, jsSize);
    5959   
    60     if (strcmp(jsBuffer, expectedValue) != 0)
    61         fprintf(stderr, "assertEqualsAsUTF8String strcmp failed: %s != %s\n", jsBuffer, expectedValue);
     60    unsigned i;
     61    for (i = 0; jsBuffer[i]; i++)
     62        if (jsBuffer[i] != expectedValue[i])
     63            fprintf(stderr, "assertEqualsAsUTF8String failed at character %d: %c(%d) != %c(%d)\n", i, jsBuffer[i], jsBuffer[i], expectedValue[i], expectedValue[i]);
    6264       
    6365    if (jsSize < strlen(jsBuffer) + 1)
     
    500502    JSValueRef v;
    501503    JSObjectRef o;
     504    JSStringRef string;
    502505
    503506    result = JSEvaluateScript(context, goodSyntax, NULL, NULL, 1, NULL);
     
    521524   
    522525    JSStringRef functionBody;
     526    JSObjectRef function;
    523527   
    524528    exception = NULL;
    525529    functionBody = JSStringCreateWithUTF8CString("rreturn Array;");
    526530    JSStringRef line = JSStringCreateWithUTF8CString("line");
    527     assert(!JSObjectMakeFunctionWithBody(context, functionBody, NULL, 1, &exception));
     531    assert(!JSObjectMakeFunctionWithBody(context, NULL, 0, NULL, functionBody, NULL, 1, &exception));
    528532    assert(JSValueIsObject(exception));
    529533    v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL);
     
    533537    JSStringRelease(line);
    534538
     539    exception = NULL;
    535540    functionBody = JSStringCreateWithUTF8CString("return Array;");
    536     JSObjectRef function = JSObjectMakeFunctionWithBody(context, functionBody, NULL, 1, NULL);
     541    function = JSObjectMakeFunctionWithBody(context, NULL, 0, NULL, functionBody, NULL, 1, &exception);
    537542    JSStringRelease(functionBody);
    538 
     543    assert(!exception);
    539544    assert(JSObjectIsFunction(function));
    540545    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
     546    assert(v);
    541547    assert(JSValueIsEqual(context, v, arrayConstructor, NULL));
    542                                                  
     548   
     549    exception = NULL;
     550    function = JSObjectMakeFunctionWithBody(context, NULL, 0, NULL, jsEmptyIString, NULL, 0, &exception);
     551    assert(!exception);
     552    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, &exception);
     553    assert(v && !exception);
     554    assert(JSValueIsUndefined(v));
     555   
     556    exception = NULL;
     557    v = NULL;
     558    JSStringRef foo = JSStringCreateWithUTF8CString("foo");
     559    JSStringRef argumentNames[] = { foo };
     560    functionBody = JSStringCreateWithUTF8CString("return foo;");
     561    function = JSObjectMakeFunctionWithBody(context, foo, 1, argumentNames, functionBody, NULL, 1, &exception);
     562    assert(function && !exception);
     563    JSValueRef arguments[] = { JSValueMakeNumber(2) };
     564    v = JSObjectCallAsFunction(context, function, NULL, 1, arguments, &exception);
     565    JSStringRelease(foo);
     566    JSStringRelease(functionBody);
     567   
     568    string = JSValueToStringCopy(context, function, NULL);
     569    assertEqualsAsUTF8String(JSValueMakeString(string), "function foo(foo) \n{\n  return foo;\n}");
     570    JSStringRelease(string);
     571
    543572    JSStringRef print = JSStringCreateWithUTF8CString("print");
    544573    JSObjectRef printFunction = JSObjectMakeFunction(context, print_callAsFunction);
     
    572601   
    573602    functionBody = JSStringCreateWithUTF8CString("return this;");
    574     function = JSObjectMakeFunctionWithBody(context, functionBody, NULL, 1, NULL);
     603    function = JSObjectMakeFunctionWithBody(context, NULL, 0, NULL, functionBody, NULL, 1, NULL);
    575604    JSStringRelease(functionBody);
    576605    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
  • trunk/JavaScriptCore/ChangeLog

    r15462 r15463  
     12006-07-15  Geoffrey Garen  <[email protected]>
     2
     3        Reviewed by Maciej.
     4       
     5        - JSObjectMakeFunctionWithBody includes a function name and named parameters now.
     6
     7        * API/JSObjectRef.cpp:
     8        (JSObjectMakeFunctionWithBody):
     9        * API/JSObjectRef.h:
     10        * API/testapi.c:
     11        (assertEqualsAsUTF8String): More informative failure reporting.
     12        (main): Test more function cases.
     13
    1142006-07-15  Geoffrey Garen  <[email protected]>
    215
Note: See TracChangeset for help on using the changeset viewer.