2018-05-31 22:31:03 +00:00
|
|
|
// Copyright (c) 2004-present, Facebook, Inc.
|
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
2016-11-01 18:38:47 +00:00
|
|
|
#include <functional>
|
2017-05-26 10:43:20 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include <jschelpers/JavaScriptCore.h>
|
|
|
|
#include <jschelpers/Value.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-06-01 03:51:10 +00:00
|
|
|
#ifndef RN_EXPORT
|
|
|
|
#define RN_EXPORT __attribute__((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2017-06-27 18:15:36 +00:00
|
|
|
class RN_EXPORT JSException : public std::exception {
|
2017-05-26 10:43:20 +00:00
|
|
|
public:
|
|
|
|
explicit JSException(const char* msg)
|
|
|
|
: msg_(msg) {}
|
2017-05-24 13:20:44 +00:00
|
|
|
|
2017-05-26 10:43:20 +00:00
|
|
|
explicit JSException(JSContextRef ctx, JSValueRef exn, const char* msg) {
|
|
|
|
buildMessage(ctx, exn, nullptr, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit JSException(JSContextRef ctx, JSValueRef exn, JSStringRef sourceURL) {
|
|
|
|
buildMessage(ctx, exn, sourceURL, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& getStack() const {
|
|
|
|
return stack_;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* what() const noexcept override {
|
|
|
|
return msg_.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string msg_;
|
|
|
|
std::string stack_;
|
|
|
|
|
|
|
|
void buildMessage(JSContextRef ctx, JSValueRef exn, JSStringRef sourceURL, const char* errorMsg);
|
|
|
|
};
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
namespace ExceptionHandling {
|
|
|
|
struct ExtractedEror {
|
|
|
|
std::string message;
|
|
|
|
// Stacktrace formatted like JS stack
|
|
|
|
// method@filename[:line[:column]]
|
|
|
|
std::string stack;
|
|
|
|
};
|
2017-12-13 20:25:10 +00:00
|
|
|
typedef ExtractedEror(*PlatformErrorExtractor)(const std::exception &ex, const char *context);
|
2017-06-14 16:16:26 +00:00
|
|
|
extern PlatformErrorExtractor platformErrorExtractor;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2017-06-01 03:51:10 +00:00
|
|
|
RN_EXPORT void installGlobalFunction(
|
2016-11-01 18:38:47 +00:00
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSFunction function);
|
|
|
|
|
|
|
|
JSObjectRef makeFunction(
|
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const char* name,
|
|
|
|
JSObjectCallAsFunctionCallback callback);
|
|
|
|
|
2017-06-26 12:10:56 +00:00
|
|
|
RN_EXPORT void installGlobalFunction(
|
2016-05-04 02:29:58 +00:00
|
|
|
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
|
|
|
|
|
2017-08-08 11:46:23 +00:00
|
|
|
/**
|
|
|
|
* A lock for protecting accesses to the JSGlobalContext
|
|
|
|
* This will be a no-op for most compilations, where #if WITH_FBJSCEXTENSIONS is false,
|
|
|
|
* but avoids deadlocks in execution environments with advanced locking requirements,
|
|
|
|
* particularly with uses of the pthread mutex lock
|
|
|
|
**/
|
|
|
|
class JSContextLock {
|
|
|
|
public:
|
|
|
|
JSContextLock(JSGlobalContextRef ctx) noexcept;
|
|
|
|
~JSContextLock() noexcept;
|
|
|
|
private:
|
|
|
|
#if WITH_FBJSCEXTENSIONS
|
|
|
|
JSGlobalContextRef ctx_;
|
|
|
|
pthread_mutex_t globalLock_;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
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
|
|
|
} }
|