Changeset 15463 in webkit for trunk/JavaScriptCore
- Timestamp:
- Jul 15, 2006, 6:40:07 PM (19 years ago)
- Location:
- trunk/JavaScriptCore
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/API/JSObjectRef.cpp
r15462 r15463 90 90 } 91 91 92 JSObjectRef JSObjectMakeFunctionWithBody(JSContextRef context, JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception) 93 { 94 JSLock lock; 95 96 ExecState* exec = toJS(context); 92 JSObjectRef 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; 97 98 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); 116 116 } 117 117 -
trunk/JavaScriptCore/API/JSObjectRef.h
r15462 r15463 398 398 @abstract Creates a function with a given script as its body. 399 399 @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. 400 403 @param body A JSString containing the script to use as the function's body. 401 404 @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. 402 405 @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. 403 406 @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 a n anonymous function, or NULL if body contains a syntax error. The returnedobject'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. 405 408 @discussion Use this method when you want to execute a script repeatedly, to avoid the cost of re-parsing the script before each execution. 406 409 */ 407 JSObjectRef JSObjectMakeFunctionWithBody(JSContextRef context, JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception);410 JSObjectRef JSObjectMakeFunctionWithBody(JSContextRef context, JSStringRef name, unsigned parameterCount, JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception); 408 411 409 412 /*! … … 521 524 @param thisObject The object to use as "this," or NULL to use the global object as "this." 522 525 @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. 524 527 @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. 525 528 @result The JSValue that results from calling object as a function, or NULL if an exception is thrown or object is not a function. … … 539 542 @param object The JSObject to call as a constructor. 540 543 @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. 542 545 @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. 543 546 @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 58 58 JSStringGetUTF8CString(valueAsString, jsBuffer, jsSize); 59 59 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]); 62 64 63 65 if (jsSize < strlen(jsBuffer) + 1) … … 500 502 JSValueRef v; 501 503 JSObjectRef o; 504 JSStringRef string; 502 505 503 506 result = JSEvaluateScript(context, goodSyntax, NULL, NULL, 1, NULL); … … 521 524 522 525 JSStringRef functionBody; 526 JSObjectRef function; 523 527 524 528 exception = NULL; 525 529 functionBody = JSStringCreateWithUTF8CString("rreturn Array;"); 526 530 JSStringRef line = JSStringCreateWithUTF8CString("line"); 527 assert(!JSObjectMakeFunctionWithBody(context, functionBody, NULL, 1, &exception));531 assert(!JSObjectMakeFunctionWithBody(context, NULL, 0, NULL, functionBody, NULL, 1, &exception)); 528 532 assert(JSValueIsObject(exception)); 529 533 v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL); … … 533 537 JSStringRelease(line); 534 538 539 exception = NULL; 535 540 functionBody = JSStringCreateWithUTF8CString("return Array;"); 536 JSObjectRef function = JSObjectMakeFunctionWithBody(context, functionBody, NULL, 1, NULL);541 function = JSObjectMakeFunctionWithBody(context, NULL, 0, NULL, functionBody, NULL, 1, &exception); 537 542 JSStringRelease(functionBody); 538 543 assert(!exception); 539 544 assert(JSObjectIsFunction(function)); 540 545 v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); 546 assert(v); 541 547 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 543 572 JSStringRef print = JSStringCreateWithUTF8CString("print"); 544 573 JSObjectRef printFunction = JSObjectMakeFunction(context, print_callAsFunction); … … 572 601 573 602 functionBody = JSStringCreateWithUTF8CString("return this;"); 574 function = JSObjectMakeFunctionWithBody(context, functionBody, NULL, 1, NULL);603 function = JSObjectMakeFunctionWithBody(context, NULL, 0, NULL, functionBody, NULL, 1, NULL); 575 604 JSStringRelease(functionBody); 576 605 v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); -
trunk/JavaScriptCore/ChangeLog
r15462 r15463 1 2006-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 1 14 2006-07-15 Geoffrey Garen <[email protected]> 2 15
Note:
See TracChangeset
for help on using the changeset viewer.