Changeset 15404 in webkit for trunk/JavaScriptCore/API/testapi.c


Ignore:
Timestamp:
Jul 13, 2006, 1:56:52 AM (19 years ago)
Author:
ggaren
Message:

Pleasing to Maciej.


  • Renamed JSEvaluate -> JSEvaluateScript, JSCheckSyntax -> JSCheckScriptSyntax
  • Added exception out parameters to JSValueTo* and JSValueIsEqual because they can throw
  • Removed JSObjectGetDescription because it's useless and vague, and JSValueToString/JSValueIsObjectOfClass do a better job, anyway
  • Clarified comments about "IsFunction/Constructor" to indicate that they are true of all functions/constructors, not just those created by JSObjectMake*
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/testapi.c

    r15385 r15404  
    3939static void assertEqualsAsBoolean(JSValueRef value, bool expectedValue)
    4040{
    41     if (JSValueToBoolean(context, value) != expectedValue)
     41    if (JSValueToBoolean(context, value, NULL) != expectedValue)
    4242        fprintf(stderr, "assertEqualsAsBoolean failed: %p, %d\n", value, expectedValue);
    4343}
     
    4545static void assertEqualsAsNumber(JSValueRef value, double expectedValue)
    4646{
    47     double number = JSValueToNumber(context, value);
     47    double number = JSValueToNumber(context, value, NULL);
    4848    if (number != expectedValue && !(isnan(number) && isnan(expectedValue)))
    4949        fprintf(stderr, "assertEqualsAsNumber failed: %p, %lf\n", value, expectedValue);
     
    5252static void assertEqualsAsUTF8String(JSValueRef value, const char* expectedValue)
    5353{
    54     JSStringRef valueAsString = JSValueToStringCopy(context, value);
     54    JSStringRef valueAsString = JSValueToStringCopy(context, value, NULL);
    5555
    5656    size_t jsSize = JSStringGetMaximumUTF8CStringSize(valueAsString);
     
    7070static void assertEqualsAsCharactersPtr(JSValueRef value, const char* expectedValue)
    7171{
    72     JSStringRef valueAsString = JSValueToStringCopy(context, value);
     72    JSStringRef valueAsString = JSValueToStringCopy(context, value, NULL);
    7373
    7474    size_t jsLength = JSStringGetLength(valueAsString);
     
    196196
    197197    if (argc > 0 && JSValueIsStrictEqual(context, argv[0], JSValueMakeNumber(0)))
    198         return JSValueToObject(context, JSValueMakeNumber(1));
    199    
    200     return JSValueToObject(context, JSValueMakeNumber(0));
     198        return JSValueToObject(context, JSValueMakeNumber(1), NULL);
     199   
     200    return JSValueToObject(context, JSValueMakeNumber(0), NULL);
    201201}
    202202
     
    206206
    207207    JSStringRef numberString = JSStringCreateWithUTF8CString("Number");
    208     JSObjectRef numberConstructor = JSValueToObject(context, JSObjectGetProperty(context, JSContextGetGlobalObject(context), numberString));
     208    JSObjectRef numberConstructor = JSValueToObject(context, JSObjectGetProperty(context, JSContextGetGlobalObject(context), numberString), NULL);
    209209    JSStringRelease(numberString);
    210210
     
    219219    switch (type) {
    220220    case kJSTypeBoolean:
    221         return JSValueMakeBoolean(false); // default object conversion is 'true'
     221        *exception = JSValueMakeNumber(2);
     222        return NULL;
    222223    case kJSTypeNumber:
    223224        return JSValueMakeNumber(1);
     
    269270   
    270271    if (argc > 0) {
    271         JSStringRef string = JSValueToStringCopy(context, argv[0]);
     272        JSStringRef string = JSValueToStringCopy(context, argv[0], NULL);
    272273        size_t sizeUTF8 = JSStringGetMaximumUTF8CStringSize(string);
    273274        char stringUTF8[sizeUTF8];
     
    302303   
    303304    context = JSContextCreate(NULL);
    304 
     305   
     306    JSObjectRef globalObject = JSContextGetGlobalObject(context);
     307    assert(JSValueIsObject(globalObject));
     308   
    305309    JSValueRef jsUndefined = JSValueMakeUndefined();
    306310    JSValueRef jsNull = JSValueMakeNull();
     
    364368#endif // __APPLE__
    365369
     370    JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL);
     371    assert(didInitialize);
     372    JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject");
     373    JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone);
     374    JSStringRelease(myObjectIString);
     375   
     376    JSValueRef exception;
     377
    366378    // Conversions that throw exceptions
    367     assert(NULL == JSValueToObject(context, jsNull));
    368     assert(isnan(JSValueToNumber(context, jsObjectNoProto)));
    369     assertEqualsAsCharactersPtr(jsObjectNoProto, "");
     379    exception = NULL;
     380    assert(NULL == JSValueToObject(context, jsNull, &exception));
     381    assert(exception);
     382   
     383    exception = NULL;
     384    assert(isnan(JSValueToNumber(context, jsObjectNoProto, &exception)));
     385    assert(exception);
     386
     387    exception = NULL;
     388    assert(!JSValueToStringCopy(context, jsObjectNoProto, &exception));
     389    assert(exception);
     390   
     391    exception = NULL;
     392    assert(!JSValueToBoolean(context, myObject, &exception));
     393    assert(exception);
     394   
     395    exception = NULL;
     396    assert(!JSValueIsEqual(context, jsObjectNoProto, JSValueMakeNumber(1), &exception));
     397    assert(exception);
    370398
    371399    assertEqualsAsBoolean(jsUndefined, false);
     
    437465    assert(!JSValueIsStrictEqual(context, jsOne, jsOneString));
    438466
    439     assert(JSValueIsEqual(context, jsOne, jsOneString));
    440     assert(!JSValueIsEqual(context, jsTrue, jsFalse));
     467    assert(JSValueIsEqual(context, jsOne, jsOneString, NULL));
     468    assert(!JSValueIsEqual(context, jsTrue, jsFalse, NULL));
    441469   
    442470#if defined(__APPLE__)
     
    458486    JSValueUnprotect(jsGlobalValue);
    459487
    460     /* JSInterpreter.h */
    461    
    462     JSObjectRef globalObject = JSContextGetGlobalObject(context);
    463     assert(JSValueIsObject(globalObject));
    464 
    465488    JSStringRef goodSyntax = JSStringCreateWithUTF8CString("x = 1;");
    466489    JSStringRef badSyntax = JSStringCreateWithUTF8CString("x := 1;");
    467     assert(JSCheckSyntax(context, goodSyntax, NULL, 0, NULL));
    468     assert(!JSCheckSyntax(context, badSyntax, NULL, 0, NULL));
     490    assert(JSCheckScriptSyntax(context, goodSyntax, NULL, 0, NULL));
     491    assert(!JSCheckScriptSyntax(context, badSyntax, NULL, 0, NULL));
    469492
    470493    JSValueRef result;
    471     JSValueRef exception;
    472494    JSValueRef v;
    473495    JSObjectRef o;
    474496
    475     result = JSEvaluate(context, goodSyntax, NULL, NULL, 1, NULL);
     497    result = JSEvaluateScript(context, goodSyntax, NULL, NULL, 1, NULL);
    476498    assert(result);
    477     assert(JSValueIsEqual(context, result, jsOne));
     499    assert(JSValueIsEqual(context, result, jsOne, NULL));
    478500
    479501    exception = NULL;
    480     result = JSEvaluate(context, badSyntax, NULL, NULL, 1, &exception);
     502    result = JSEvaluateScript(context, badSyntax, NULL, NULL, 1, &exception);
    481503    assert(!result);
    482504    assert(JSValueIsObject(exception));
     
    485507    v = JSObjectGetProperty(context, globalObject, array);
    486508    assert(v);
    487     JSObjectRef arrayConstructor = JSValueToObject(context, v);
     509    JSObjectRef arrayConstructor = JSValueToObject(context, v, NULL);
    488510    JSStringRelease(array);
    489511    result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL);
     
    499521    assert(!JSObjectMakeFunctionWithBody(context, functionBody, NULL, 1, &exception));
    500522    assert(JSValueIsObject(exception));
    501     v = JSObjectGetProperty(context, JSValueToObject(context, exception), line);
     523    v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line);
    502524    assert(v);
    503525    assertEqualsAsNumber(v, 2); // FIXME: Lexer::setCode bumps startingLineNumber by 1 -- we need to change internal callers so that it doesn't have to (saying '0' to mean '1' in the API would be really confusing -- it's really confusing internally, in fact)
     
    511533    assert(JSObjectIsFunction(function));
    512534    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
    513     assert(JSValueIsEqual(context, v, arrayConstructor));
     535    assert(JSValueIsEqual(context, v, arrayConstructor, NULL));
    514536                                                 
    515     JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL);
    516     assert(didInitialize);
    517     JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject");
    518     JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone);
    519     JSStringRelease(myObjectIString);
    520 
    521537    JSStringRef print = JSStringCreateWithUTF8CString("print");
    522538    JSObjectRef printFunction = JSObjectMakeFunction(context, print_callAsFunction);
     
    552568    JSStringRelease(functionBody);
    553569    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
    554     assert(JSValueIsEqual(context, v, globalObject));
     570    assert(JSValueIsEqual(context, v, globalObject, NULL));
    555571    v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL);
    556     assert(JSValueIsEqual(context, v, o));
     572    assert(JSValueIsEqual(context, v, o, NULL));
    557573   
    558574    char* scriptUTF8 = createStringWithContentsOfFile("testapi.js");
    559575    JSStringRef script = JSStringCreateWithUTF8CString(scriptUTF8);
    560     result = JSEvaluate(context, script, NULL, NULL, 1, &exception);
     576    result = JSEvaluateScript(context, script, NULL, NULL, 1, &exception);
    561577    if (JSValueIsUndefined(result))
    562578        printf("PASS: Test script executed successfully.\n");
    563579    else {
    564580        printf("FAIL: Test script returned unexcpected value:\n");
    565         JSStringRef exceptionIString = JSValueToStringCopy(context, exception);
     581        JSStringRef exceptionIString = JSValueToStringCopy(context, exception, NULL);
    566582        CFStringRef exceptionCF = JSStringCopyCFString(kCFAllocatorDefault, exceptionIString);
    567583        CFShow(exceptionCF);
Note: See TracChangeset for help on using the changeset viewer.