JS4D::ValueRef JS4D::VValueToValue( ContextRef inContext, const VValueSingle& inValue, ValueRef *outException) { ValueRef jsValue; switch( inValue.GetValueKind()) { case VK_BYTE: case VK_WORD: case VK_LONG: case VK_LONG8: case VK_REAL: case VK_FLOAT: case VK_SUBTABLE_KEY: jsValue = inValue.IsNull() ? JSValueMakeNull( inContext) : JSValueMakeNumber( inContext, inValue.GetReal()); break; case VK_BOOLEAN: jsValue = inValue.IsNull() ? JSValueMakeNull( inContext) : JSValueMakeBoolean( inContext, inValue.GetBoolean() != 0); break; case VK_UUID: if (inValue.IsNull()) jsValue = JSValueMakeNull( inContext) ; else { VString tempS; inValue.GetString(tempS); jsValue = VStringToValue( inContext, tempS); } break; case VK_TEXT: case VK_STRING: #if VERSIONDEBUG jsValue = VStringToValue( inContext, dynamic_cast<const VString&>( inValue)); #else jsValue = VStringToValue( inContext, (const VString&)( inValue)); #endif break; case VK_TIME: #if VERSIONDEBUG jsValue = inValue.IsNull() ? JSValueMakeNull( inContext) : VTimeToObject( inContext, dynamic_cast<const VTime&>( inValue), outException); #else jsValue = inValue.IsNull() ? JSValueMakeNull( inContext) : VTimeToObject( inContext, (const VTime&)( inValue), outException); #endif break; case VK_DURATION: #if VERSIONDEBUG jsValue = VDurationToValue( inContext, dynamic_cast<const VDuration&>( inValue)); #else jsValue = VDurationToValue( inContext, (const VDuration&)( inValue)); #endif break; case VK_BLOB: jsValue = VBlobToValue( inContext, (const VBlob&)( inValue)); break; case VK_IMAGE: #if !VERSION_LINUX // Postponed Linux Implementation ! jsValue = VPictureToValue(inContext, (const VPicture&)( inValue)); #endif break; default: jsValue = JSValueMakeUndefined( inContext); // exception instead ? break; } return jsValue; }
JSValueRef function_load(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { // TODO: implement fully if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char path[PATH_MAX]; JSStringRef path_str = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str) < PATH_MAX); JSStringGetUTF8CString(path_str, path, PATH_MAX); JSStringRelease(path_str); // debug_print_value("load", ctx, args[0]); time_t last_modified = 0; char *contents = NULL; char *loaded_path = strdup(path); bool developing = (config.num_src_paths == 1 && strcmp(config.src_paths[0].type, "src") == 0 && str_has_suffix(config.src_paths[0].path, "/planck-cljs/src/") == 0); if (!developing) { contents = bundle_get_contents(path); last_modified = 0; } // load from classpath if (contents == NULL) { for (int i = 0; i < config.num_src_paths; i++) { if (config.src_paths[i].blacklisted) { continue; } char *type = config.src_paths[i].type; char *location = config.src_paths[i].path; if (strcmp(type, "src") == 0) { char *full_path = str_concat(location, path); contents = get_contents(full_path, &last_modified); if (contents != NULL) { free(loaded_path); loaded_path = strdup(full_path); } free(full_path); } else if (strcmp(type, "jar") == 0) { struct stat file_stat; if (stat(location, &file_stat) == 0) { contents = get_contents_zip(location, path, &last_modified); } else { cljs_perror(location); config.src_paths[i].blacklisted = true; } } if (contents != NULL) { break; } } } // load from out/ if (contents == NULL) { if (config.out_path != NULL) { char *full_path = str_concat(config.out_path, path); contents = get_contents(full_path, &last_modified); free(full_path); } } if (developing && contents == NULL) { contents = bundle_get_contents(path); last_modified = 0; } if (contents != NULL) { JSStringRef contents_str = JSStringCreateWithUTF8CString(contents); free(contents); JSStringRef loaded_path_str = JSStringCreateWithUTF8CString(loaded_path); free(loaded_path); JSValueRef res[3]; res[0] = JSValueMakeString(ctx, contents_str); res[1] = JSValueMakeNumber(ctx, last_modified); res[2] = JSValueMakeString(ctx, loaded_path_str); return JSObjectMakeArray(ctx, 3, res, NULL); } } return JSValueMakeNull(ctx); }
template<> JSValueRef RJSAccessor::from_float(JSContextRef ctx, float f) { return JSValueMakeNumber(ctx, f); }
JSValueRef JSCInt16Array::BYTES_PER_ELEMENTConstGetter(JSContextRef ctx, JSObjectRef thisObj, JSStringRef propertyName, JSValueRef *exception) { return JSValueMakeNumber(ctx, 2); }
JSValueRef function_high_res_timer(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { return JSValueMakeNumber(ctx, 1e-6 * system_time()); }
EJ_BIND_GET(EJBindingHttpRequest, status, ctx) { return JSValueMakeNumber( ctx, getStatusCode() ); }
static JSValueRef getPixmapHeight(JSContextRef context, JSObjectRef object, JSStringRef, JSValueRef*) { QVariant& data = *static_cast<QVariant*>(JSObjectGetPrivate(object)); return JSValueMakeNumber(context, imageSizeForVariant(data).height()); }
static JSValueRef indexInTableCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { return JSValueMakeNumber(context, toAXElement(thisObject)->indexInTable()); }
static JSValueRef getMaxValueCallback(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception) { return JSValueMakeNumber(context, toAXElement(thisObject)->maxValue()); }
jsc::Value::Value(Context &context, double number) : context(context) { value = JSValueMakeNumber(context, number); JSValueProtect(context, value); }
JSValueRef JSCCharacterData::lengthAttrGetter(JSContextRef ctx, JSObjectRef thisObj, JSStringRef propertyName, JSValueRef* exception) { struct JSCCharacterDataPrivate* privData = static_cast<JSCCharacterData::JSCCharacterDataPrivate* >(JSObjectGetPrivate(thisObj)); return JSValueMakeNumber(ctx, privData->arabicaThis->getLength()); }
EJ_BIND_GET( EJBindingImageData, height, ctx ) { return JSValueMakeNumber( ctx, m_imageData->height ); }
EJ_BIND_GET( EJBindingImageData, width, ctx ) { return JSValueMakeNumber( ctx, m_imageData->width); }
JSValueRef jsvalue_from_number(JSContextRef ctx, double number) { return JSValueMakeNumber(ctx, number); }
/** * return the NaN value */ JSValueRef GetNaNForJSBuffers (JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { return JSValueMakeNumber(ctx, NAN); }
static JSValueRef getInsertionPointLineNumberCallback(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception) { return JSValueMakeNumber(context, toAXElement(thisObject)->insertionPointLineNumber()); }
EJ_BIND_GET(EJBindingHttpRequest, readyState, ctx) { return JSValueMakeNumber( ctx, state ); }
static JSValueRef hierarchicalLevelCallback(JSContextRef context, JSObjectRef thisObject, JSStringRef, JSValueRef*) { return JSValueMakeNumber(context, toAXElement(thisObject)->hierarchicalLevel()); }
EJ_BIND_GET(EJBindingHttpRequest, timeout, ctx) { return JSValueMakeNumber( ctx, timeout ); }
JSValue::JSValue(int64_t val) : ctx_(GetJSContext()) { instance_ = JSValueMakeNumber(ctx_, static_cast<double>(val)); JSValueProtect(ctx_, instance_); }
JSValueRef JSCInt16Array::lengthAttrGetter(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef *exception) { struct JSCInt16ArrayPrivate* privData = (struct JSCInt16ArrayPrivate*)JSObjectGetPrivate(object); return JSValueMakeNumber(ctx, privData->nativeObj->getLength()); }
JSValue::JSValue(double val) : ctx_(GetJSContext()) { instance_ = JSValueMakeNumber(ctx_, val); JSValueProtect(ctx_, instance_); }
int main(int argc, char* argv[]) { const char *scriptPath = "testapi.js"; if (argc > 1) { scriptPath = argv[1]; } // Test garbage collection with a fresh context context = JSGlobalContextCreateInGroup(NULL, NULL); TestInitializeFinalize = true; testInitializeFinalize(); JSGlobalContextRelease(context); TestInitializeFinalize = false; ASSERT(Base_didFinalize); JSClassDefinition globalObjectClassDefinition = kJSClassDefinitionEmpty; globalObjectClassDefinition.initialize = globalObject_initialize; globalObjectClassDefinition.staticValues = globalObject_staticValues; globalObjectClassDefinition.staticFunctions = globalObject_staticFunctions; globalObjectClassDefinition.attributes = kJSClassAttributeNoAutomaticPrototype; JSClassRef globalObjectClass = JSClassCreate(&globalObjectClassDefinition); context = JSGlobalContextCreateInGroup(NULL, globalObjectClass); JSGlobalContextRetain(context); JSGlobalContextRelease(context); JSReportExtraMemoryCost(context, 0); JSReportExtraMemoryCost(context, 1); JSReportExtraMemoryCost(context, 1024); JSObjectRef globalObject = JSContextGetGlobalObject(context); ASSERT(JSValueIsObject(context, globalObject)); JSValueRef jsUndefined = JSValueMakeUndefined(context); JSValueRef jsNull = JSValueMakeNull(context); JSValueRef jsTrue = JSValueMakeBoolean(context, true); JSValueRef jsFalse = JSValueMakeBoolean(context, false); JSValueRef jsZero = JSValueMakeNumber(context, 0); JSValueRef jsOne = JSValueMakeNumber(context, 1); JSValueRef jsOneThird = JSValueMakeNumber(context, 1.0 / 3.0); JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, NULL); JSObjectSetPrototype(context, jsObjectNoProto, JSValueMakeNull(context)); // FIXME: test funny utf8 characters JSStringRef jsEmptyIString = JSStringCreateWithUTF8CString(""); JSValueRef jsEmptyString = JSValueMakeString(context, jsEmptyIString); JSStringRef jsOneIString = JSStringCreateWithUTF8CString("1"); JSValueRef jsOneString = JSValueMakeString(context, jsOneIString); UniChar singleUniChar = 65; // Capital A CFMutableStringRef cfString = CFStringCreateMutableWithExternalCharactersNoCopy(kCFAllocatorDefault, &singleUniChar, 1, 1, kCFAllocatorNull); JSStringRef jsCFIString = JSStringCreateWithCFString(cfString); JSValueRef jsCFString = JSValueMakeString(context, jsCFIString); CFStringRef cfEmptyString = CFStringCreateWithCString(kCFAllocatorDefault, "", kCFStringEncodingUTF8); JSStringRef jsCFEmptyIString = JSStringCreateWithCFString(cfEmptyString); JSValueRef jsCFEmptyString = JSValueMakeString(context, jsCFEmptyIString); CFIndex cfStringLength = CFStringGetLength(cfString); UniChar* buffer = (UniChar*)malloc(cfStringLength * sizeof(UniChar)); CFStringGetCharacters(cfString, CFRangeMake(0, cfStringLength), buffer); JSStringRef jsCFIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, cfStringLength); JSValueRef jsCFStringWithCharacters = JSValueMakeString(context, jsCFIStringWithCharacters); JSStringRef jsCFEmptyIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, CFStringGetLength(cfEmptyString)); free(buffer); JSValueRef jsCFEmptyStringWithCharacters = JSValueMakeString(context, jsCFEmptyIStringWithCharacters); ASSERT(JSValueGetType(context, jsUndefined) == kJSTypeUndefined); ASSERT(JSValueGetType(context, jsNull) == kJSTypeNull); ASSERT(JSValueGetType(context, jsTrue) == kJSTypeBoolean); ASSERT(JSValueGetType(context, jsFalse) == kJSTypeBoolean); ASSERT(JSValueGetType(context, jsZero) == kJSTypeNumber); ASSERT(JSValueGetType(context, jsOne) == kJSTypeNumber); ASSERT(JSValueGetType(context, jsOneThird) == kJSTypeNumber); ASSERT(JSValueGetType(context, jsEmptyString) == kJSTypeString); ASSERT(JSValueGetType(context, jsOneString) == kJSTypeString); ASSERT(JSValueGetType(context, jsCFString) == kJSTypeString); ASSERT(JSValueGetType(context, jsCFStringWithCharacters) == kJSTypeString); ASSERT(JSValueGetType(context, jsCFEmptyString) == kJSTypeString); ASSERT(JSValueGetType(context, jsCFEmptyStringWithCharacters) == kJSTypeString); JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL); JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject"); JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone, NULL); JSStringRelease(myObjectIString); JSObjectRef EvilExceptionObject = JSObjectMake(context, EvilExceptionObject_class(context), NULL); JSStringRef EvilExceptionObjectIString = JSStringCreateWithUTF8CString("EvilExceptionObject"); JSObjectSetProperty(context, globalObject, EvilExceptionObjectIString, EvilExceptionObject, kJSPropertyAttributeNone, NULL); JSStringRelease(EvilExceptionObjectIString); JSObjectRef EmptyObject = JSObjectMake(context, EmptyObject_class(context), NULL); JSStringRef EmptyObjectIString = JSStringCreateWithUTF8CString("EmptyObject"); JSObjectSetProperty(context, globalObject, EmptyObjectIString, EmptyObject, kJSPropertyAttributeNone, NULL); JSStringRelease(EmptyObjectIString); JSValueRef exception; // Conversions that throw exceptions exception = NULL; ASSERT(NULL == JSValueToObject(context, jsNull, &exception)); ASSERT(exception); exception = NULL; // FIXME <rdar://4668451> - On i386 the isnan(double) macro tries to map to the isnan(float) function, // causing a build break with -Wshorten-64-to-32 enabled. The issue is known by the appropriate team. // After that's resolved, we can remove these casts ASSERT(isnan((float)JSValueToNumber(context, jsObjectNoProto, &exception))); ASSERT(exception); exception = NULL; ASSERT(!JSValueToStringCopy(context, jsObjectNoProto, &exception)); ASSERT(exception); ASSERT(JSValueToBoolean(context, myObject)); exception = NULL; ASSERT(!JSValueIsEqual(context, jsObjectNoProto, JSValueMakeNumber(context, 1), &exception)); ASSERT(exception); exception = NULL; JSObjectGetPropertyAtIndex(context, myObject, 0, &exception); ASSERT(1 == JSValueToNumber(context, exception, NULL)); 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;\n}"); 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); if (timeZoneIsPST()) 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); // Verify that creating a constructor for a class with no static functions does not trigger // an assert inside putDirect or lead to a crash during GC. <https://bugs.webkit.org/show_bug.cgi?id=25785> nullDefinition = kJSClassDefinitionEmpty; nullClass = JSClassCreate(&nullDefinition); myConstructor = JSObjectMakeConstructor(context, nullClass, 0); JSClassRelease(nullClass); char* scriptUTF8 = createStringWithContentsOfFile(scriptPath); if (!scriptUTF8) { printf("FAIL: Test script could not be loaded.\n"); failed = 1; } 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); failed = 1; } 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; myConstructor = NULL; JSStringRelease(jsEmptyIString); JSStringRelease(jsOneIString); JSStringRelease(jsCFIString); JSStringRelease(jsCFEmptyIString); JSStringRelease(jsCFIStringWithCharacters); JSStringRelease(jsCFEmptyIStringWithCharacters); JSStringRelease(goodSyntax); JSStringRelease(badSyntax); JSGlobalContextRelease(context); JSClassRelease(globalObjectClass); // Test for an infinite prototype chain that used to be created. This test // passes if the call to JSObjectHasProperty() does not hang. JSClassDefinition prototypeLoopClassDefinition = kJSClassDefinitionEmpty; prototypeLoopClassDefinition.staticFunctions = globalObject_staticFunctions; JSClassRef prototypeLoopClass = JSClassCreate(&prototypeLoopClassDefinition); JSGlobalContextRef prototypeLoopContext = JSGlobalContextCreateInGroup(NULL, prototypeLoopClass); JSStringRef nameProperty = JSStringCreateWithUTF8CString("name"); JSObjectHasProperty(prototypeLoopContext, JSContextGetGlobalObject(prototypeLoopContext), nameProperty); JSGlobalContextRelease(prototypeLoopContext); JSClassRelease(prototypeLoopClass); printf("PASS: Infinite prototype chain does not occur.\n"); if (failed) { printf("FAIL: Some tests failed.\n"); return 1; } printf("PASS: Program exited normally.\n"); return 0; }
static JSValueRef getWebHistoryItemCountCallback(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception) { LayoutTestController* controller = static_cast<LayoutTestController*>(JSObjectGetPrivate(thisObject)); return JSValueMakeNumber(context, controller->webHistoryItemCount()); }
JSValueRef function_fstat(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *path = value_to_c_string(ctx, args[0]); struct stat file_stat; int retval = lstat(path, &file_stat); if (retval == 0) { JSObjectRef result = JSObjectMake(ctx, NULL, NULL); char *type = "unknown"; if (S_ISDIR(file_stat.st_mode)) { type = "directory"; } else if (S_ISREG(file_stat.st_mode)) { type = "file"; } else if (S_ISLNK(file_stat.st_mode)) { type = "symbolic-link"; } else if (S_ISSOCK(file_stat.st_mode)) { type = "socket"; } else if (S_ISFIFO(file_stat.st_mode)) { type = "fifo"; } else if (S_ISCHR(file_stat.st_mode)) { type = "character-special"; } else if (S_ISBLK(file_stat.st_mode)) { type = "block-special"; } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("type"), c_string_to_value(ctx, type), kJSPropertyAttributeReadOnly, NULL); double device_id = (double) file_stat.st_rdev; if (device_id) { JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("device-id"), JSValueMakeNumber(ctx, device_id), kJSPropertyAttributeReadOnly, NULL); } double file_number = (double) file_stat.st_ino; if (file_number) { JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("file-number"), JSValueMakeNumber(ctx, file_number), kJSPropertyAttributeReadOnly, NULL); } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("permissions"), JSValueMakeNumber(ctx, (double) (ACCESSPERMS & file_stat.st_mode)), kJSPropertyAttributeReadOnly, NULL); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("reference-count"), JSValueMakeNumber(ctx, (double) file_stat.st_nlink), kJSPropertyAttributeReadOnly, NULL); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("uid"), JSValueMakeNumber(ctx, (double) file_stat.st_uid), kJSPropertyAttributeReadOnly, NULL); struct passwd *uid_passwd = getpwuid(file_stat.st_uid); if (uid_passwd) { JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("uname"), c_string_to_value(ctx, uid_passwd->pw_name), kJSPropertyAttributeReadOnly, NULL); } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("gid"), JSValueMakeNumber(ctx, (double) file_stat.st_gid), kJSPropertyAttributeReadOnly, NULL); struct group *gid_group = getgrgid(file_stat.st_gid); if (gid_group) { JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("gname"), c_string_to_value(ctx, gid_group->gr_name), kJSPropertyAttributeReadOnly, NULL); } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("file-size"), JSValueMakeNumber(ctx, (double) file_stat.st_size), kJSPropertyAttributeReadOnly, NULL); #ifdef __APPLE__ #define birthtime(x) x.st_birthtime #else #define birthtime(x) x.st_ctime #endif JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("created"), JSValueMakeNumber(ctx, 1000 * birthtime(file_stat)), kJSPropertyAttributeReadOnly, NULL); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("modified"), JSValueMakeNumber(ctx, 1000 * file_stat.st_mtime), kJSPropertyAttributeReadOnly, NULL); return result; } free(path); } return JSValueMakeNull(ctx); }
pdf_jsimp_obj *pdf_jsimp_from_number(pdf_jsimp *imp, double num) { return wrap_val(imp, JSValueMakeNumber(imp->jscore_ctx, num)); }
template<> JSValueRef RJSAccessor::from_long(JSContextRef ctx, long long l) { return JSValueMakeNumber(ctx, l); }
/** * return the length of the buffer */ JSValueRef GetLengthForJSBuffers (JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { BUFFER(buffer); return JSValueMakeNumber(ctx, buffer->length); }
template<> JSValueRef RJSAccessor::from_double(JSContextRef ctx, double d) { return JSValueMakeNumber(ctx, d); }
NX::Value::Value (JSContextRef context, double value): myContext(context), myVal(nullptr) { myVal = JSValueMakeNumber(myContext, value); JSValueProtect(context, myVal); }