⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testapi.c

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 C
📖 第 1 页 / 共 3 页
字号:
    assertEqualsAsBoolean(jsUndefined, false);    assertEqualsAsBoolean(jsNull, false);    assertEqualsAsBoolean(jsTrue, true);    assertEqualsAsBoolean(jsFalse, false);    assertEqualsAsBoolean(jsZero, false);    assertEqualsAsBoolean(jsOne, true);    assertEqualsAsBoolean(jsOneThird, true);    assertEqualsAsBoolean(jsEmptyString, false);    assertEqualsAsBoolean(jsOneString, true);    assertEqualsAsBoolean(jsCFString, true);    assertEqualsAsBoolean(jsCFStringWithCharacters, true);    assertEqualsAsBoolean(jsCFEmptyString, false);    assertEqualsAsBoolean(jsCFEmptyStringWithCharacters, false);        assertEqualsAsNumber(jsUndefined, nan(""));    assertEqualsAsNumber(jsNull, 0);    assertEqualsAsNumber(jsTrue, 1);    assertEqualsAsNumber(jsFalse, 0);    assertEqualsAsNumber(jsZero, 0);    assertEqualsAsNumber(jsOne, 1);    assertEqualsAsNumber(jsOneThird, 1.0 / 3.0);    assertEqualsAsNumber(jsEmptyString, 0);    assertEqualsAsNumber(jsOneString, 1);    assertEqualsAsNumber(jsCFString, nan(""));    assertEqualsAsNumber(jsCFStringWithCharacters, nan(""));    assertEqualsAsNumber(jsCFEmptyString, 0);    assertEqualsAsNumber(jsCFEmptyStringWithCharacters, 0);    ASSERT(sizeof(JSChar) == sizeof(UniChar));        assertEqualsAsCharactersPtr(jsUndefined, "undefined");    assertEqualsAsCharactersPtr(jsNull, "null");    assertEqualsAsCharactersPtr(jsTrue, "true");    assertEqualsAsCharactersPtr(jsFalse, "false");    assertEqualsAsCharactersPtr(jsZero, "0");    assertEqualsAsCharactersPtr(jsOne, "1");    assertEqualsAsCharactersPtr(jsOneThird, "0.3333333333333333");    assertEqualsAsCharactersPtr(jsEmptyString, "");    assertEqualsAsCharactersPtr(jsOneString, "1");    assertEqualsAsCharactersPtr(jsCFString, "A");    assertEqualsAsCharactersPtr(jsCFStringWithCharacters, "A");    assertEqualsAsCharactersPtr(jsCFEmptyString, "");    assertEqualsAsCharactersPtr(jsCFEmptyStringWithCharacters, "");        assertEqualsAsUTF8String(jsUndefined, "undefined");    assertEqualsAsUTF8String(jsNull, "null");    assertEqualsAsUTF8String(jsTrue, "true");    assertEqualsAsUTF8String(jsFalse, "false");    assertEqualsAsUTF8String(jsZero, "0");    assertEqualsAsUTF8String(jsOne, "1");    assertEqualsAsUTF8String(jsOneThird, "0.3333333333333333");    assertEqualsAsUTF8String(jsEmptyString, "");    assertEqualsAsUTF8String(jsOneString, "1");    assertEqualsAsUTF8String(jsCFString, "A");    assertEqualsAsUTF8String(jsCFStringWithCharacters, "A");    assertEqualsAsUTF8String(jsCFEmptyString, "");    assertEqualsAsUTF8String(jsCFEmptyStringWithCharacters, "");        ASSERT(JSValueIsStrictEqual(context, jsTrue, jsTrue));    ASSERT(!JSValueIsStrictEqual(context, jsOne, jsOneString));    ASSERT(JSValueIsEqual(context, jsOne, jsOneString, NULL));    ASSERT(!JSValueIsEqual(context, jsTrue, jsFalse, NULL));        CFStringRef cfJSString = JSStringCopyCFString(kCFAllocatorDefault, jsCFIString);    CFStringRef cfJSEmptyString = JSStringCopyCFString(kCFAllocatorDefault, jsCFEmptyIString);    ASSERT(CFEqual(cfJSString, cfString));    ASSERT(CFEqual(cfJSEmptyString, cfEmptyString));    CFRelease(cfJSString);    CFRelease(cfJSEmptyString);    CFRelease(cfString);    CFRelease(cfEmptyString);        jsGlobalValue = JSObjectMake(context, NULL, NULL);    JSValueProtect(context, jsGlobalValue);    JSGarbageCollect(context);    ASSERT(JSValueIsObject(context, jsGlobalValue));    JSValueUnprotect(context, jsGlobalValue);    JSStringRef goodSyntax = JSStringCreateWithUTF8CString("x = 1;");    JSStringRef badSyntax = JSStringCreateWithUTF8CString("x := 1;");    ASSERT(JSCheckScriptSyntax(context, goodSyntax, NULL, 0, NULL));    ASSERT(!JSCheckScriptSyntax(context, badSyntax, NULL, 0, NULL));    JSValueRef result;    JSValueRef v;    JSObjectRef o;    JSStringRef string;    result = JSEvaluateScript(context, goodSyntax, NULL, NULL, 1, NULL);    ASSERT(result);    ASSERT(JSValueIsEqual(context, result, jsOne, NULL));    exception = NULL;    result = JSEvaluateScript(context, badSyntax, NULL, NULL, 1, &exception);    ASSERT(!result);    ASSERT(JSValueIsObject(context, exception));        JSStringRef array = JSStringCreateWithUTF8CString("Array");    JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL);    JSStringRelease(array);    result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL);    ASSERT(result);    ASSERT(JSValueIsObject(context, result));    ASSERT(JSValueIsInstanceOfConstructor(context, result, arrayConstructor, NULL));    ASSERT(!JSValueIsInstanceOfConstructor(context, JSValueMakeNull(context), arrayConstructor, NULL));    o = JSValueToObject(context, result, NULL);    exception = NULL;    ASSERT(JSValueIsUndefined(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception)));    ASSERT(!exception);        JSObjectSetPropertyAtIndex(context, o, 0, JSValueMakeNumber(context, 1), &exception);    ASSERT(!exception);        exception = NULL;    ASSERT(1 == JSValueToNumber(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception), &exception));    ASSERT(!exception);    JSStringRef functionBody;    JSObjectRef function;        exception = NULL;    functionBody = JSStringCreateWithUTF8CString("rreturn Array;");    JSStringRef line = JSStringCreateWithUTF8CString("line");    ASSERT(!JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception));    ASSERT(JSValueIsObject(context, exception));    v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL);    assertEqualsAsNumber(v, 1);    JSStringRelease(functionBody);    JSStringRelease(line);    exception = NULL;    functionBody = JSStringCreateWithUTF8CString("return Array;");    function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception);    JSStringRelease(functionBody);    ASSERT(!exception);    ASSERT(JSObjectIsFunction(context, function));    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);    ASSERT(v);    ASSERT(JSValueIsEqual(context, v, arrayConstructor, NULL));        exception = NULL;    function = JSObjectMakeFunction(context, NULL, 0, NULL, jsEmptyIString, NULL, 0, &exception);    ASSERT(!exception);    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, &exception);    ASSERT(v && !exception);    ASSERT(JSValueIsUndefined(context, v));        exception = NULL;    v = NULL;    JSStringRef foo = JSStringCreateWithUTF8CString("foo");    JSStringRef argumentNames[] = { foo };    functionBody = JSStringCreateWithUTF8CString("return foo;");    function = JSObjectMakeFunction(context, foo, 1, argumentNames, functionBody, NULL, 1, &exception);    ASSERT(function && !exception);    JSValueRef arguments[] = { JSValueMakeNumber(context, 2) };    v = JSObjectCallAsFunction(context, function, NULL, 1, arguments, &exception);    JSStringRelease(foo);    JSStringRelease(functionBody);        string = JSValueToStringCopy(context, function, NULL);    assertEqualsAsUTF8String(JSValueMakeString(context, string), "function foo(foo) {return foo;}");    JSStringRelease(string);    JSStringRef print = JSStringCreateWithUTF8CString("print");    JSObjectRef printFunction = JSObjectMakeFunctionWithCallback(context, print, print_callAsFunction);    JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone, NULL);     JSStringRelease(print);        ASSERT(!JSObjectSetPrivate(printFunction, (void*)1));    ASSERT(!JSObjectGetPrivate(printFunction));    JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor");    JSObjectRef myConstructor = JSObjectMakeConstructor(context, NULL, myConstructor_callAsConstructor);    JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone, NULL);    JSStringRelease(myConstructorIString);        ASSERT(!JSObjectSetPrivate(myConstructor, (void*)1));    ASSERT(!JSObjectGetPrivate(myConstructor));        string = JSStringCreateWithUTF8CString("Derived");    JSObjectRef derivedConstructor = JSObjectMakeConstructor(context, Derived_class(context), NULL);    JSObjectSetProperty(context, globalObject, string, derivedConstructor, kJSPropertyAttributeNone, NULL);    JSStringRelease(string);        o = JSObjectMake(context, NULL, NULL);    JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeNone, NULL);    JSObjectSetProperty(context, o, jsCFIString,  JSValueMakeNumber(context, 1), kJSPropertyAttributeDontEnum, NULL);    JSPropertyNameArrayRef nameArray = JSObjectCopyPropertyNames(context, o);    size_t expectedCount = JSPropertyNameArrayGetCount(nameArray);    size_t count;    for (count = 0; count < expectedCount; ++count)        JSPropertyNameArrayGetNameAtIndex(nameArray, count);    JSPropertyNameArrayRelease(nameArray);    ASSERT(count == 1); // jsCFString should not be enumerated    JSValueRef argumentsArrayValues[] = { JSValueMakeNumber(context, 10), JSValueMakeNumber(context, 20) };    o = JSObjectMakeArray(context, sizeof(argumentsArrayValues) / sizeof(JSValueRef), argumentsArrayValues, NULL);    string = JSStringCreateWithUTF8CString("length");    v = JSObjectGetProperty(context, o, string, NULL);    assertEqualsAsNumber(v, 2);    v = JSObjectGetPropertyAtIndex(context, o, 0, NULL);    assertEqualsAsNumber(v, 10);    v = JSObjectGetPropertyAtIndex(context, o, 1, NULL);    assertEqualsAsNumber(v, 20);    o = JSObjectMakeArray(context, 0, NULL, NULL);    v = JSObjectGetProperty(context, o, string, NULL);    assertEqualsAsNumber(v, 0);    JSStringRelease(string);    JSValueRef argumentsDateValues[] = { JSValueMakeNumber(context, 0) };    o = JSObjectMakeDate(context, 1, argumentsDateValues, NULL);    assertEqualsAsUTF8String(o, "Wed Dec 31 1969 16:00:00 GMT-0800 (PST)");    string = JSStringCreateWithUTF8CString("an error message");    JSValueRef argumentsErrorValues[] = { JSValueMakeString(context, string) };    o = JSObjectMakeError(context, 1, argumentsErrorValues, NULL);    assertEqualsAsUTF8String(o, "Error: an error message");    JSStringRelease(string);    string = JSStringCreateWithUTF8CString("foo");    JSStringRef string2 = JSStringCreateWithUTF8CString("gi");    JSValueRef argumentsRegExpValues[] = { JSValueMakeString(context, string), JSValueMakeString(context, string2) };    o = JSObjectMakeRegExp(context, 2, argumentsRegExpValues, NULL);    assertEqualsAsUTF8String(o, "/foo/gi");    JSStringRelease(string);    JSStringRelease(string2);    JSClassDefinition nullDefinition = kJSClassDefinitionEmpty;    nullDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;    JSClassRef nullClass = JSClassCreate(&nullDefinition);    JSClassRelease(nullClass);        nullDefinition = kJSClassDefinitionEmpty;    nullClass = JSClassCreate(&nullDefinition);    JSClassRelease(nullClass);    functionBody = JSStringCreateWithUTF8CString("return this;");    function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL);    JSStringRelease(functionBody);    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);    ASSERT(JSValueIsEqual(context, v, globalObject, NULL));    v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL);    ASSERT(JSValueIsEqual(context, v, o, NULL));    functionBody = JSStringCreateWithUTF8CString("return eval(\"this\");");    function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL);    JSStringRelease(functionBody);    v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);    ASSERT(JSValueIsEqual(context, v, globalObject, NULL));    v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL);    ASSERT(JSValueIsEqual(context, v, o, NULL));    JSStringRef script = JSStringCreateWithUTF8CString("this;");    v = JSEvaluateScript(context, script, NULL, NULL, 1, NULL);    ASSERT(JSValueIsEqual(context, v, globalObject, NULL));    v = JSEvaluateScript(context, script, o, NULL, 1, NULL);    ASSERT(JSValueIsEqual(context, v, o, NULL));    JSStringRelease(script);    script = JSStringCreateWithUTF8CString("eval(this);");    v = JSEvaluateScript(context, script, NULL, NULL, 1, NULL);    ASSERT(JSValueIsEqual(context, v, globalObject, NULL));    v = JSEvaluateScript(context, script, o, NULL, 1, NULL);    ASSERT(JSValueIsEqual(context, v, o, NULL));    JSStringRelease(script);    char* scriptUTF8 = createStringWithContentsOfFile(scriptPath);    if (!scriptUTF8)        printf("FAIL: Test script could not be loaded.\n");    else {        script = JSStringCreateWithUTF8CString(scriptUTF8);        result = JSEvaluateScript(context, script, NULL, NULL, 1, &exception);        if (JSValueIsUndefined(context, result))            printf("PASS: Test script executed successfully.\n");        else {            printf("FAIL: Test script returned unexpected value:\n");            JSStringRef exceptionIString = JSValueToStringCopy(context, exception, NULL);            CFStringRef exceptionCF = JSStringCopyCFString(kCFAllocatorDefault, exceptionIString);            CFShow(exceptionCF);            CFRelease(exceptionCF);            JSStringRelease(exceptionIString);        }        JSStringRelease(script);        free(scriptUTF8);    }    // Clear out local variables pointing at JSObjectRefs to allow their values to be collected    function = NULL;    v = NULL;    o = NULL;    globalObject = NULL;    JSStringRelease(jsEmptyIString);    JSStringRelease(jsOneIString);    JSStringRelease(jsCFIString);    JSStringRelease(jsCFEmptyIString);    JSStringRelease(jsCFIStringWithCharacters);    JSStringRelease(jsCFEmptyIStringWithCharacters);    JSStringRelease(goodSyntax);    JSStringRelease(badSyntax);    JSGlobalContextRelease(context);    JSClassRelease(globalObjectClass);    printf("PASS: Program exited normally.\n");    return 0;}static char* createStringWithContentsOfFile(const char* fileName){    char* buffer;        size_t buffer_size = 0;    size_t buffer_capacity = 1024;    buffer = (char*)malloc(buffer_capacity);        FILE* f = fopen(fileName, "r");    if (!f) {        fprintf(stderr, "Could not open file: %s\n", fileName);        return 0;    }        while (!feof(f) && !ferror(f)) {        buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);        if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'            buffer_capacity *= 2;            buffer = (char*)realloc(buffer, buffer_capacity);            ASSERT(buffer);        }                ASSERT(buffer_size < buffer_capacity);    }    fclose(f);    buffer[buffer_size] = '\0';        return buffer;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -