2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-11-22 14:05:36 +00:00
|
|
|
#include <jschelpers/Value.h>
|
|
|
|
#include <jschelpers/JavaScriptCore.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <algorithm>
|
2016-11-01 18:38:47 +00:00
|
|
|
#include <functional>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
inline void throwJSExecutionException(const char* msg) {
|
2016-06-16 21:28:28 +00:00
|
|
|
throw JSException(msg);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
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...);
|
2016-06-16 21:28:28 +00:00
|
|
|
throw JSException(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
inline void throwJSExecutionExceptionWithStack(const char* msg, const char* stack) {
|
|
|
|
throw JSException(msg, stack);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 18:38:47 +00:00
|
|
|
using JSFunction = std::function<JSValueRef(JSContextRef, JSObjectRef, size_t, const JSValueRef[])>;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
void installGlobalFunction(
|
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSObjectCallAsFunctionCallback callback);
|
|
|
|
|
2016-10-11 14:19:31 +00:00
|
|
|
void installGlobalProxy(
|
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSObjectGetPropertyCallback callback);
|
|
|
|
|
2016-11-02 19:18:11 +00:00
|
|
|
void removeGlobal(JSGlobalContextRef ctx, const char* name);
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef evaluateScript(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSStringRef script,
|
|
|
|
JSStringRef sourceURL);
|
|
|
|
|
2016-07-11 13:52:06 +00:00
|
|
|
#if WITH_FBJSCEXTENSIONS
|
|
|
|
JSValueRef evaluateSourceCode(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSSourceCodeRef source,
|
|
|
|
JSStringRef sourceURL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void formatAndThrowJSException(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSValueRef exn,
|
|
|
|
JSStringRef sourceURL);
|
|
|
|
|
2016-05-10 12:02:56 +00:00
|
|
|
JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, const char *exceptionLocation);
|
2016-07-07 11:48:45 +00:00
|
|
|
JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, JSObjectRef jsFunctionCause);
|
|
|
|
|
|
|
|
template<JSValueRef (method)(JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef *exception)>
|
|
|
|
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);
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueMakeUndefined(ctx);
|
2016-07-07 11:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return &funcWrapper::call;
|
|
|
|
}
|
2016-05-10 12:02:56 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
} }
|