// Copyright 2004-present Facebook. All Rights Reserved. #pragma once #include #include #include #include #include namespace facebook { namespace react { inline void throwJSExecutionException(const char* msg) { throw JSException(msg); } template inline void throwJSExecutionException(const char* fmt, Args... args) { int msgSize = snprintf(nullptr, 0, fmt, args...); msgSize = std::min(512, msgSize + 1); char *msg = (char*) alloca(msgSize); snprintf(msg, msgSize, fmt, args...); throw JSException(msg); } template inline void throwJSExecutionExceptionWithStack(const char* msg, const char* stack) { throw JSException(msg, stack); } using JSFunction = std::function; JSObjectRef makeFunction( JSContextRef ctx, const char* name, JSFunction function); void installGlobalFunction( JSGlobalContextRef ctx, const char* name, JSFunction function); JSObjectRef makeFunction( JSGlobalContextRef ctx, const char* name, JSObjectCallAsFunctionCallback callback); void installGlobalFunction( JSGlobalContextRef ctx, const char* name, JSObjectCallAsFunctionCallback callback); void installGlobalProxy( JSGlobalContextRef ctx, const char* name, JSObjectGetPropertyCallback callback); void removeGlobal(JSGlobalContextRef ctx, const char* name); JSValueRef evaluateScript( JSContextRef ctx, JSStringRef script, JSStringRef sourceURL); #if WITH_FBJSCEXTENSIONS JSValueRef evaluateSourceCode( JSContextRef ctx, JSSourceCodeRef source, JSStringRef sourceURL); #endif void formatAndThrowJSException( JSContextRef ctx, JSValueRef exn, JSStringRef sourceURL); JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, const char *exceptionLocation); JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, JSObjectRef jsFunctionCause); template inline JSObjectCallAsFunctionCallback exceptionWrapMethod() { struct funcWrapper { static JSValueRef call( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { try { return (*method)(ctx, function, thisObject, argumentCount, arguments, exception); } catch (...) { *exception = translatePendingCppExceptionToJSError(ctx, function); return JSC_JSValueMakeUndefined(ctx); } } }; return &funcWrapper::call; } } }