int main() { JSContextRef ctx = JSGlobalContextCreate(NULL); JSValueRef jsvalue; JSObjectRef jsobject2; JSObjectRef jsobject3; JSValueRef jsvalue4; jsvalue = JSValueMakeNumber(ctx, 0.123456711111111111); print_js(ctx, jsvalue); jsvalue = JSValueMakeBoolean(ctx, 1); print_js(ctx, jsvalue); jsvalue = JSValueMakeBoolean(ctx, 0); print_js(ctx, jsvalue); jsvalue = JSValueMakeNull(ctx); print_js(ctx, jsvalue); jsvalue = JSValueMakeUndefined(ctx); /* JSObjectIsFunction(ctx, jsvalue); //segmentation fault */ print_js(ctx, jsvalue); jsvalue = JSObjectMakeError(ctx, 0, NULL, NULL); printf("%lx\n", (unsigned long)jsvalue); printf("%lx\n", (unsigned long)(jsvalue = JSValueToObject(ctx, jsvalue, NULL))); printf("%lx\n", (unsigned long)(jsvalue = JSValueToObject(ctx, jsvalue, NULL))); print_js(ctx, jsvalue); jsvalue = JSObjectMake(ctx, NULL, NULL); print_js(ctx, jsvalue); jsobject2 = (JSObjectRef)get_property(ctx, jsvalue, "toString"); print_js(ctx, jsobject2); jsobject3 = JSObjectMakeError(ctx, 0, NULL, NULL); /* jsobject3 = JSValueMakeBoolean(ctx, 0); */ /* jsobject3 = JSValueMakeNull(ctx); */ /* jsobject3 = JSValueMakeNumber(ctx, 0.123134123); */ /* jsobject3 = JSValueMakeUndefined(ctx); */ jsvalue4 = JSObjectCallAsFunction(ctx, jsobject2, jsobject3, 0, NULL, NULL); print_js(ctx, jsvalue4); printf("test_function\n"); //test_function(ctx, jsobject3); JSStringRef jsstr; jsstr = JSStringCreateWithUTF8CString("abcdef"); jsvalue = JSValueMakeString(ctx, jsstr); JSStringRelease(jsstr); printf("%lx\n", (unsigned long)jsvalue); print_js(ctx, jsvalue); printf("%lx\n", (unsigned long)JSValueToObject(ctx, jsvalue, NULL)); print_js(ctx, JSValueToObject(ctx, jsvalue, NULL)); JSObjectCopyPropertyNames(ctx, (JSObjectRef)jsvalue); return 0; }
NATIVE(JSObject,jobject,makeError) (PARAMS, jlong ctx, jlongArray args) { JSValueRef exception = NULL; int i, sum = 0; jsize len = env->GetArrayLength(args); jlong *values = env->GetLongArrayElements(args, 0); JSValueRef* elements = new JSValueRef[len]; for (i=0; i<len; i++) { elements[i] = (JSValueRef) values[i]; } env->ReleaseLongArrayElements(args, values, 0); jclass ret = env->FindClass("org/liquidplayer/webkit/javascriptcore/JNIReturnObject"); jmethodID cid = env->GetMethodID(ret,"<init>","()V"); jobject out = env->NewObject(ret, cid); jfieldID fid = env->GetFieldID(ret , "reference", "J"); env->SetLongField( out, fid, (long) JSObjectMakeError((JSContextRef) ctx, (size_t)len, (len==0)?NULL:elements, &exception)); fid = env->GetFieldID(ret , "exception", "J"); env->SetLongField( out, fid, (long) exception); delete elements; return out; }
/** * create an Error object using message */ EXPORTAPI JSValueRef HyperloopMakeException(JSContextRef ctx, const char *error) { auto string = JSStringCreateWithUTF8CString(error); auto message = JSValueMakeString(ctx, string); JSStringRelease(string); return JSObjectMakeError(ctx, 1, &message, 0); }
JSObjectRef MakeInstance (JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { size_t length = 1; if (argumentCount > 0) { *exception = nullptr; if (!JSValueIsNumber(ctx,arguments[0])) { JSStringRef string = JSStringCreateWithUTF8CString("required first argument to be a number which is the size of the buffer requested"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return nullptr; } length = JSValueToNumber(ctx, arguments[0], exception); CHECK_BUF_EXCEPTION(nullptr); } JSBuffer *buffer = (JSBuffer *)malloc(sizeof(JSBuffer)); buffer->buffer = malloc(length); buffer->length = length; buffer->type = JSBufferTypePointer; memset(buffer->buffer, 0, length); // by default, let's set the value to NAN float *p = (float*)buffer->buffer; p[0] = NAN; JSObjectRef object = MakeObjectForJSBuffer(ctx,buffer); return object; }
JSValueRef makeJSError(JSContextRef ctx, const char *error) { JSValueRef nestedException = nullptr; JSValueRef args[] = { Value(ctx, String(error)) }; JSObjectRef errorObj = JSObjectMakeError(ctx, 1, args, &nestedException); if (nestedException != nullptr) { return std::move(args[0]); } return errorObj; }
/** * slice */ JSValueRef sliceForJSBuffer (JSContextRef ctx, JSObjectRef function, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { BUFFER(buffer); MIN_SIZE(2); GET_NUMBER(0,index); GET_NUMBER(1,length); if (length > buffer->length) { JSStringRef string = JSStringCreateWithUTF8CString("length requested is greater than internal buffer length"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return JSValueMakeUndefined(ctx); } if ((int)index >= buffer->length || (int)index < 0) { JSStringRef string = JSStringCreateWithUTF8CString("index requested is invalid"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return JSValueMakeUndefined(ctx); } if (buffer->type!=JSBufferTypePointer) { JSStringRef string = JSStringCreateWithUTF8CString("cannot slice a non-pointer buffer"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return JSValueMakeUndefined(ctx); } throw ref new Exception(0, "JSBuffer's sliceForJSBuffer has not been implemented on Windows yet."); /*void *memory = malloc(length); void *start = &(buffer->buffer[(int)index]); memcpy(memory,start,length); JSBuffer *newbuffer = (JSBuffer *)malloc(sizeof(JSBuffer)); newbuffer->buffer = memory; newbuffer->length = length; newbuffer->type = JSBufferTypePointer; return MakeObjectForJSBuffer(ctx,newbuffer);*/ }
bool JS4D::ConvertErrorContextToException( ContextRef inContext, VErrorContext *inErrorContext, ExceptionRef *outException) { bool exceptionGenerated; if ( (inErrorContext != NULL) && !inErrorContext->IsEmpty() && (outException != NULL) ) { exceptionGenerated = true; VErrorBase *error = inErrorContext->GetLast(); if ( (error != NULL) && (*outException == NULL) ) // don't override existing exception { VString description; for( VErrorStack::const_reverse_iterator i = inErrorContext->GetErrorStack().rbegin() ; description.IsEmpty() && (i != inErrorContext->GetErrorStack().rend()) ; ++i) { (*i)->GetErrorDescription( description); } JSStringRef jsString = JS4D::VStringToString( description); JSValueRef argumentsErrorValues[] = { JSValueMakeString( inContext, jsString) }; JSStringRelease( jsString); *outException = JSObjectMakeError( inContext, 1, argumentsErrorValues, NULL); if ( *outException != NULL ) { VJSObject e(inContext, JSValueToObject(inContext, *outException, nil)); VJSArray arr(inContext); for( VErrorStack::const_reverse_iterator i = inErrorContext->GetErrorStack().rbegin() ; (i != inErrorContext->GetErrorStack().rend()) ; ++i) { (*i)->GetErrorDescription( description); VJSValue jsdesc(inContext); jsdesc.SetString(description); arr.PushValue(jsdesc); } e.SetProperty("messages", arr, PropertyAttributeNone); } } } else { exceptionGenerated = false; } return exceptionGenerated; }
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; }
/** * put */ JSValueRef putForJSBuffer (JSContextRef ctx, JSObjectRef function, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { BUFFER(buffer); ARGCOUNT(4); JSValueRef bufValueRef = arguments[0]; if (!JSValueIsObject(ctx,bufValueRef)) { JSStringRef string = JSStringCreateWithUTF8CString("first argument must be a buffer object"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return JSValueMakeUndefined(ctx); } JSObjectRef bufObjectRef = JSValueToObject(ctx,bufValueRef,exception); CHECK_EXCEPTION_UNDEFINED JSBuffer *srcBuffer = (JSBuffer*)HyperloopGetPrivateObjectAsJSBuffer(bufObjectRef); if (srcBuffer==nullptr) { JSStringRef string = JSStringCreateWithUTF8CString("first argument must be a buffer object (JSBuffer nullptr)"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return JSValueMakeUndefined(ctx); } GET_NUMBER(1,srcIndex); GET_NUMBER(2,srcLength); GET_NUMBER(3,destIndex); if (srcLength > srcBuffer->length) { JSStringRef string = JSStringCreateWithUTF8CString("source length passed in greater than source buffer length"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return JSValueMakeUndefined(ctx); } if (srcLength <= 0) { JSStringRef string = JSStringCreateWithUTF8CString("source length must be greater than 0"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return JSValueMakeUndefined(ctx); } throw ref new Exception(0, "JSBuffer's putForJSBuffer has not been implemented on Windows yet."); /*void *src = &(srcBuffer->buffer[(int)srcIndex]); size_t newsize = (buffer->length - (int)destIndex); newsize = newsize + srcLength - newsize; void *dest = &(buffer->buffer[(int)destIndex]); if (newsize > buffer->length) { // new to grow it void *copy = malloc(buffer->length); size_t copylen = buffer->length; memcpy(copy, buffer->buffer, copylen); free(buffer->buffer); buffer->buffer = malloc(newsize); buffer->length = newsize; memcpy(buffer->buffer,copy,copylen); } memcpy(dest, src, (int)srcLength); return object;*/ }
{ "log", [](JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) -> JSValueRef { try { std::string output; for(int i = 0; i < argumentCount; i++) { output += NX::Value(ctx, arguments[i]).toString(); if (i < argumentCount - 1) output += " "; } output += "\n"; std::cout << output; } catch(const std::exception & e) { NX::Value message(ctx, e.what()); JSValueRef args[] { message.value(), nullptr }; *exception = JSObjectMakeError(ctx, 1, args, nullptr); } return JSValueMakeUndefined(ctx); }, 0 }, { "error", [](JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) -> JSValueRef { try { std::string output; for(int i = 0; i < argumentCount; i++) { output += NX::Value(ctx, arguments[i]).toString(); if (i < argumentCount - 1) output += " "; } output += "\n";