2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
2017-06-14 16:16:26 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "JSCHelpers.h"
|
|
|
|
|
2016-11-01 18:38:43 +00:00
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
#include <fbsystrace.h>
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2017-08-08 11:46:23 +00:00
|
|
|
#if WITH_FBJSCEXTENSIONS
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2016-11-22 14:05:36 +00:00
|
|
|
#include "JavaScriptCore.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "Value.h"
|
2017-09-19 01:35:44 +00:00
|
|
|
#include <privatedata/PrivateDataBase.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-08-08 11:46:23 +00:00
|
|
|
#if WITH_FBJSCEXTENSIONS
|
|
|
|
#undef ASSERT
|
|
|
|
#undef WTF_EXPORT_PRIVATE
|
|
|
|
|
|
|
|
#include <JavaScriptCore/config.h>
|
|
|
|
#include <wtf/WTFThreadData.h>
|
|
|
|
|
|
|
|
#undef TRUE
|
|
|
|
#undef FALSE
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2016-11-01 18:38:47 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-09-19 01:35:44 +00:00
|
|
|
class JSFunctionPrivateData : public PrivateDataBase {
|
|
|
|
public:
|
|
|
|
explicit JSFunctionPrivateData(JSFunction&& function) : jsFunction_{std::move(function)} {}
|
|
|
|
|
|
|
|
JSFunction& getJSFunction() {
|
|
|
|
return jsFunction_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSFunction jsFunction_;
|
|
|
|
};
|
|
|
|
|
2016-11-01 18:38:47 +00:00
|
|
|
JSValueRef functionCaller(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
2016-11-22 14:05:38 +00:00
|
|
|
const bool isCustomJSC = isCustomJSCPtr(ctx);
|
2017-09-19 01:35:44 +00:00
|
|
|
auto* privateData = PrivateDataBase::cast<JSFunctionPrivateData>(
|
|
|
|
JSC_JSObjectGetPrivate(isCustomJSC, function));
|
|
|
|
return (privateData->getJSFunction())(ctx, thisObject, argumentCount, arguments);
|
2016-11-01 18:38:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 14:05:38 +00:00
|
|
|
JSClassRef createFuncClass(JSContextRef ctx) {
|
2017-01-25 19:33:06 +00:00
|
|
|
JSClassDefinition definition = kJSClassDefinitionEmpty;
|
|
|
|
definition.attributes |= kJSClassAttributeNoAutomaticPrototype;
|
2017-07-24 11:49:37 +00:00
|
|
|
|
2016-11-22 14:05:38 +00:00
|
|
|
// Need to duplicate the two different finalizer blocks, since there's no way
|
|
|
|
// for it to capture this static information.
|
2017-07-24 11:49:37 +00:00
|
|
|
const bool isCustomJSC = isCustomJSCPtr(ctx);
|
|
|
|
if (isCustomJSC) {
|
2016-11-22 14:05:38 +00:00
|
|
|
definition.finalize = [](JSObjectRef object) {
|
2017-09-19 01:35:44 +00:00
|
|
|
auto* privateData = PrivateDataBase::cast<JSFunctionPrivateData>(
|
|
|
|
JSC_JSObjectGetPrivate(true, object));
|
|
|
|
delete privateData;
|
2016-11-22 14:05:38 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
definition.finalize = [](JSObjectRef object) {
|
2017-09-19 01:35:44 +00:00
|
|
|
auto* privateData = PrivateDataBase::cast<JSFunctionPrivateData>(
|
|
|
|
JSC_JSObjectGetPrivate(false, object));
|
|
|
|
delete privateData;
|
2016-11-22 14:05:38 +00:00
|
|
|
};
|
|
|
|
}
|
2016-11-01 18:38:47 +00:00
|
|
|
definition.callAsFunction = exceptionWrapMethod<&functionCaller>();
|
|
|
|
|
2017-07-24 11:49:37 +00:00
|
|
|
return JSC_JSClassCreate(isCustomJSC, &definition);
|
2016-11-01 18:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObjectRef makeFunction(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSStringRef name,
|
|
|
|
JSFunction function) {
|
2016-11-22 14:05:38 +00:00
|
|
|
static JSClassRef kClassDef = NULL, kCustomJSCClassDef = NULL;
|
|
|
|
JSClassRef *classRef = isCustomJSCPtr(ctx) ? &kCustomJSCClassDef : &kClassDef;
|
|
|
|
if (!*classRef) {
|
|
|
|
*classRef = createFuncClass(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// dealloc in kClassDef.finalize
|
2017-09-19 01:35:44 +00:00
|
|
|
JSFunctionPrivateData *functionDataPtr = new JSFunctionPrivateData(std::move(function));
|
|
|
|
auto functionObject = Object(ctx, JSC_JSObjectMake(ctx, *classRef, functionDataPtr));
|
2016-11-01 18:38:47 +00:00
|
|
|
functionObject.setProperty("name", Value(ctx, name));
|
|
|
|
return functionObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-26 10:43:20 +00:00
|
|
|
void JSException::buildMessage(JSContextRef ctx, JSValueRef exn, JSStringRef sourceURL, const char* errorMsg) {
|
|
|
|
std::ostringstream msgBuilder;
|
|
|
|
if (errorMsg && strlen(errorMsg) > 0) {
|
|
|
|
msgBuilder << errorMsg << ": ";
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
Object exnObject = Value(ctx, exn).asObject();
|
|
|
|
Value exnMessage = exnObject.getProperty("message");
|
|
|
|
msgBuilder << (exnMessage.isString() ? exnMessage : (Value)exnObject).toString().str();
|
2017-05-26 10:43:20 +00:00
|
|
|
|
|
|
|
// The null/empty-ness of source tells us if the JS came from a
|
|
|
|
// file/resource, or was a constructed statement. The location
|
|
|
|
// info will include that source, if any.
|
|
|
|
std::string locationInfo = sourceURL != nullptr ? String::ref(ctx, sourceURL).str() : "";
|
|
|
|
auto line = exnObject.getProperty("line");
|
|
|
|
if (line != nullptr && line.isNumber()) {
|
|
|
|
if (locationInfo.empty() && line.asInteger() != 1) {
|
|
|
|
// If there is a non-trivial line number, but there was no
|
|
|
|
// location info, we include a placeholder, and the line
|
|
|
|
// number.
|
|
|
|
locationInfo = folly::to<std::string>("<unknown file>:", line.asInteger());
|
|
|
|
} else if (!locationInfo.empty()) {
|
|
|
|
// If there is location info, we always include the line
|
|
|
|
// number, regardless of its value.
|
|
|
|
locationInfo += folly::to<std::string>(":", line.asInteger());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!locationInfo.empty()) {
|
|
|
|
msgBuilder << " (" << locationInfo << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto exceptionText = msgBuilder.str();
|
|
|
|
LOG(ERROR) << "Got JS Exception: " << exceptionText;
|
|
|
|
msg_ = std::move(exceptionText);
|
|
|
|
|
|
|
|
Value jsStack = exnObject.getProperty("stack");
|
|
|
|
if (jsStack.isString()) {
|
|
|
|
auto stackText = jsStack.toString().str();
|
|
|
|
LOG(ERROR) << "Got JS Stack: " << stackText;
|
|
|
|
stack_ = std::move(stackText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
namespace ExceptionHandling {
|
|
|
|
|
|
|
|
PlatformErrorExtractor platformErrorExtractor;
|
|
|
|
|
|
|
|
}
|
2017-05-26 10:43:20 +00:00
|
|
|
|
2016-11-01 18:38:47 +00:00
|
|
|
JSObjectRef makeFunction(
|
|
|
|
JSContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSFunction function) {
|
2016-11-18 14:25:29 +00:00
|
|
|
return makeFunction(ctx, String(ctx, name), std::move(function));
|
2016-11-01 18:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void installGlobalFunction(
|
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSFunction function) {
|
2016-11-18 14:25:29 +00:00
|
|
|
auto jsName = String(ctx, name);
|
2016-11-01 18:38:47 +00:00
|
|
|
auto functionObj = makeFunction(ctx, jsName, std::move(function));
|
2016-11-18 14:25:24 +00:00
|
|
|
Object::getGlobalObject(ctx).setProperty(jsName, Value(ctx, functionObj));
|
2016-11-01 18:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObjectRef makeFunction(
|
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSObjectCallAsFunctionCallback callback) {
|
2016-11-18 14:25:29 +00:00
|
|
|
auto jsName = String(ctx, name);
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSObjectMakeFunctionWithCallback(ctx, jsName, callback);
|
2016-11-01 18:38:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
void installGlobalFunction(
|
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSObjectCallAsFunctionCallback callback) {
|
2016-11-18 14:25:29 +00:00
|
|
|
String jsName(ctx, name);
|
2016-11-22 14:05:38 +00:00
|
|
|
JSObjectRef functionObj = JSC_JSObjectMakeFunctionWithCallback(
|
2016-11-18 14:25:29 +00:00
|
|
|
ctx, jsName, callback);
|
2016-11-18 14:25:24 +00:00
|
|
|
Object::getGlobalObject(ctx).setProperty(jsName, Value(ctx, functionObj));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:19:31 +00:00
|
|
|
void installGlobalProxy(
|
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSObjectGetPropertyCallback callback) {
|
|
|
|
JSClassDefinition proxyClassDefintion = kJSClassDefinitionEmpty;
|
2017-01-25 19:33:06 +00:00
|
|
|
proxyClassDefintion.attributes |= kJSClassAttributeNoAutomaticPrototype;
|
2016-10-11 14:19:31 +00:00
|
|
|
proxyClassDefintion.getProperty = callback;
|
|
|
|
|
2016-11-22 14:05:38 +00:00
|
|
|
const bool isCustomJSC = isCustomJSCPtr(ctx);
|
|
|
|
JSClassRef proxyClass = JSC_JSClassCreate(isCustomJSC, &proxyClassDefintion);
|
|
|
|
JSObjectRef proxyObj = JSC_JSObjectMake(ctx, proxyClass, nullptr);
|
|
|
|
JSC_JSClassRelease(isCustomJSC, proxyClass);
|
2016-10-11 14:19:31 +00:00
|
|
|
|
2016-11-18 14:25:24 +00:00
|
|
|
Object::getGlobalObject(ctx).setProperty(name, Value(ctx, proxyObj));
|
2016-11-02 19:18:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:25:24 +00:00
|
|
|
void removeGlobal(JSGlobalContextRef ctx, const char* name) {
|
|
|
|
Object::getGlobalObject(ctx).setProperty(name, Value::makeUndefined(ctx));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 10:43:20 +00:00
|
|
|
JSValueRef evaluateScript(JSContextRef context, JSStringRef script, JSStringRef sourceURL) {
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef exn, result;
|
2017-05-26 10:43:20 +00:00
|
|
|
result = JSC_JSEvaluateScript(context, script, NULL, sourceURL, 0, &exn);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (result == nullptr) {
|
2017-05-26 10:43:20 +00:00
|
|
|
throw JSException(context, exn, sourceURL);
|
2016-07-11 13:52:06 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WITH_FBJSCEXTENSIONS
|
|
|
|
JSValueRef evaluateSourceCode(JSContextRef context, JSSourceCodeRef source, JSStringRef sourceURL) {
|
|
|
|
JSValueRef exn, result;
|
|
|
|
result = JSEvaluateSourceCode(context, source, NULL, &exn);
|
|
|
|
if (result == nullptr) {
|
2017-05-26 10:43:20 +00:00
|
|
|
throw JSException(context, exn, sourceURL);
|
2016-07-11 13:52:06 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2016-07-07 15:37:27 +00:00
|
|
|
|
2017-08-08 11:46:23 +00:00
|
|
|
JSContextLock::JSContextLock(JSGlobalContextRef ctx) noexcept
|
|
|
|
#if WITH_FBJSCEXTENSIONS
|
|
|
|
: ctx_(ctx),
|
|
|
|
globalLock_(PTHREAD_MUTEX_INITIALIZER)
|
|
|
|
{
|
|
|
|
WTFThreadData& threadData = wtfThreadData();
|
|
|
|
|
|
|
|
// Code below is responsible for acquiring locks. It should execute
|
|
|
|
// atomically, thus none of the functions invoked from now on are allowed to
|
|
|
|
// throw an exception
|
|
|
|
try {
|
|
|
|
if (!threadData.isDebuggerThread()) {
|
|
|
|
CHECK(0 == pthread_mutex_lock(&globalLock_));
|
|
|
|
}
|
|
|
|
JSLock(ctx_);
|
|
|
|
} catch (...) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
JSContextLock::~JSContextLock() noexcept {
|
|
|
|
#if WITH_FBJSCEXTENSIONS
|
|
|
|
WTFThreadData& threadData = wtfThreadData();
|
|
|
|
|
|
|
|
JSUnlock(ctx_);
|
|
|
|
if (!threadData.isDebuggerThread()) {
|
|
|
|
CHECK(0 == pthread_mutex_unlock(&globalLock_));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-10 12:02:56 +00:00
|
|
|
JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, const char *exceptionLocation) {
|
|
|
|
try {
|
|
|
|
throw;
|
|
|
|
} catch (const std::bad_alloc& ex) {
|
|
|
|
throw; // We probably shouldn't try to handle this in JS
|
|
|
|
} catch (const std::exception& ex) {
|
2017-06-14 16:16:26 +00:00
|
|
|
if (ExceptionHandling::platformErrorExtractor) {
|
|
|
|
auto extractedEror = ExceptionHandling::platformErrorExtractor(ex, exceptionLocation);
|
|
|
|
if (extractedEror.message.length() > 0) {
|
|
|
|
return Value::makeError(ctx, extractedEror.message.c_str(), extractedEror.stack.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto msg = folly::to<std::string>("C++ exception in '", exceptionLocation, "'\n\n", ex.what());
|
|
|
|
return Value::makeError(ctx, msg.c_str());
|
2016-05-10 12:02:56 +00:00
|
|
|
} catch (const char* ex) {
|
2017-06-14 16:16:26 +00:00
|
|
|
auto msg = folly::to<std::string>("C++ exception (thrown as a char*) in '", exceptionLocation, "'\n\n", ex);
|
|
|
|
return Value::makeError(ctx, msg.c_str());
|
2016-05-10 12:02:56 +00:00
|
|
|
} catch (...) {
|
2017-06-14 16:16:26 +00:00
|
|
|
auto msg = folly::to<std::string>("Unknown C++ exception in '", exceptionLocation, "'");
|
|
|
|
return Value::makeError(ctx, msg.c_str());
|
2016-05-10 12:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:48:45 +00:00
|
|
|
JSValueRef translatePendingCppExceptionToJSError(JSContextRef ctx, JSObjectRef jsFunctionCause) {
|
|
|
|
try {
|
|
|
|
auto functionName = Object(ctx, jsFunctionCause).getProperty("name").toString().str();
|
|
|
|
return translatePendingCppExceptionToJSError(ctx, functionName.c_str());
|
|
|
|
} catch (...) {
|
2017-06-14 16:16:26 +00:00
|
|
|
return Value::makeError(ctx, "Failed to translate native exception");
|
2016-07-07 11:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
} }
|