Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
chakrashim: Removed cross context shim code
Browse files Browse the repository at this point in the history
Since Chakra's JSRT APIs now support cross context scenarios,
removed the chakra shim code that was simulating the cross context behaviour.

Reviewed-By : Jianchun Xu
  • Loading branch information
kunalspathak committed Jul 23, 2015
1 parent 87862dc commit 8851bb6
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 1,112 deletions.
3 changes: 0 additions & 3 deletions deps/chakrashim/chakrashim.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
'src/jsrtcontextcachedobj.inc',
'src/jsrtcontextshim.cc',
'src/jsrtcontextshim.h',
'src/jsrtcrosscontext.cc',
'src/jsrtcrosscontext.h',
'src/jsrtisolateshim.cc',
'src/jsrtisolateshim.h',
'src/jsrtpromise.cc',
Expand Down Expand Up @@ -91,7 +89,6 @@
'src/v8object.cc',
'src/v8objecttemplate.cc',
'src/v8persistent.cc',
'src/v8returnvalue.cc',
'src/v8script.cc',
'src/v8signature.cc',
'src/v8stacktrace.cc',
Expand Down
21 changes: 7 additions & 14 deletions deps/chakrashim/include/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,6 @@ EXPORT void SetObjectWeakReferenceCallback(
// A helper method for turning off the WeakReferenceCallback that was set using
// the previous method
EXPORT void ClearObjectWeakReferenceCallback(JsValueRef object, bool revive);

EXPORT JsValueRef MarshalJsValueRefToContext(
JsValueRef value, JsContextRef context);
}

template <class T>
Expand Down Expand Up @@ -944,12 +941,10 @@ class ReturnValue {
public:
// Handle setters
template <typename S> void Set(const Persistent<S>& handle) {
*_value = static_cast<Value *>(
chakrashim::MarshalJsValueRefToContext(*handle, *_context));
*_value = static_cast<Value *>(*handle);
}
template <typename S> void Set(const Handle<S> handle) {
*_value = static_cast<Value *>(
chakrashim::MarshalJsValueRefToContext(*handle, *_context));
*_value = static_cast<Value *>(*handle);
}
// Fast primitive setters
void Set(bool value) { Set(Boolean::New(Isolate::GetCurrent(), value)); }
Expand All @@ -967,14 +962,12 @@ class ReturnValue {

Value* Get() const { return *_value; }
private:
ReturnValue(Value** value, Handle<Context> context)
: _value(value), _context(context) {
ReturnValue(Value** value)
: _value(value) {
}

Value** _value;
Local<Context> _context;

void SetCrossContext(JsValueRef valueRef);
//Local<Context> _context;

template <typename F> friend class FunctionCallbackInfo;
template <typename F> friend class PropertyCallbackInfo;
Expand All @@ -996,7 +989,7 @@ class FunctionCallbackInfo {
Isolate* GetIsolate() const { return Isolate::GetCurrent(); }
ReturnValue<T> GetReturnValue() const {
return ReturnValue<T>(
&(const_cast<FunctionCallbackInfo<T>*>(this)->_returnValue), _context);
&(const_cast<FunctionCallbackInfo<T>*>(this)->_returnValue));
}

FunctionCallbackInfo(
Expand Down Expand Up @@ -1040,7 +1033,7 @@ class PropertyCallbackInfo {
Local<Object> Holder() const { return _holder; }
ReturnValue<T> GetReturnValue() const {
return ReturnValue<T>(
&(const_cast<PropertyCallbackInfo<T>*>(this)->_returnValue), _context);
&(const_cast<PropertyCallbackInfo<T>*>(this)->_returnValue));
}

PropertyCallbackInfo(
Expand Down
143 changes: 29 additions & 114 deletions deps/chakrashim/src/jsrtcontextshim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,15 @@ ContextShim::~ContextShim() {
if (globalObjectTemplateInstance != JS_INVALID_REFERENCE) {
JsRelease(globalObjectTemplateInstance, nullptr);
}

// Mark each existing CrossContextMapInfo* that it no longer needs to
// unregister from this ContextShim.
std::for_each(crossContextObjects.begin(), crossContextObjects.end(),
[](const auto& pair) {
auto& v = pair.second;
std::for_each(v.begin(), v.end(), [](CrossContextMapInfo* info) {
info->fromContext = nullptr;
});
});
}

bool ContextShim::CheckConfigGlobalObjectTemplate() {
if (globalObjectTemplateInstance != JS_INVALID_REFERENCE) {
// Only need to config once. Discard globalObjectTemplateInstance
JsValueRef target = globalObjectTemplateInstance;
JsValueRef newProto = globalObjectTemplateInstance;
JsRelease(globalObjectTemplateInstance, nullptr);
globalObjectTemplateInstance = JS_INVALID_REFERENCE;

JsValueRef newProto = JS_INVALID_REFERENCE;
{
ContextShim* fromContext =
IsolateShim::GetCurrent()->GetJsValueRefContextShim(target);
ContextShim::Scope from(fromContext);
newProto = MarshalJsValueRefToContext(target, fromContext, this);
}

JsValueRef glob, oldProto;
return newProto != JS_INVALID_REFERENCE &&
JsGetGlobalObject(&glob) == JsNoError &&
Expand Down Expand Up @@ -219,12 +201,12 @@ bool ContextShim::InitializeGlobalPrototypeFunctions() {
// Replace the builtin function with a cross context shim function
JsValueRef function;
if (JsCreateFunction(jsrt::PrototypeFunctionCrossContextShim,
reinterpret_cast<void*>(index),
&function) != JsNoError) {
return false;
reinterpret_cast<void*>(index),
&function) != JsNoError) {
return false;
}
return JsSetProperty(prototype, functionIdRef, function,
false) == JsNoError;
false) == JsNoError;
};

struct TypeMethodPair {
Expand Down Expand Up @@ -459,97 +441,6 @@ bool ContextShim::ExecuteChakraShimJS() {
initFunction, arguments, _countof(arguments), &result) == JsNoError;
}

bool ContextShim::RegisterCrossContextObject(JsValueRef fakeTarget,
const CrossContextMapInfo& info) {
// Ensure fakeTarget lifetime encloses proxy lifetime
if (JsSetProperty(fakeTarget,
isolateShim->GetProxySymbolPropertyIdRef(),
info.proxy, false) != JsNoError) {
return false;
}

try {
CrossContextMapInfo* mapInfoCopy = new CrossContextMapInfo(info);

// Install a finalizer to clean up the map entry when proxy/fakeTarget are
// collected by GC.
JsValueRef finalizeObj;
if (JsCreateExternalObject(mapInfoCopy,
CrossContextFakeTargetFinalizeCallback,
&finalizeObj) != JsNoError) {
delete mapInfoCopy;
return false;
}

if (JsSetProperty(fakeTarget,
isolateShim->GetFinalizerSymbolPropertyIdRef(),
finalizeObj, false) != JsNoError) {
return false;
}

crossContextObjects[info.object].push_back(mapInfoCopy);
return true; // success
} catch (const std::bad_alloc&) {
return false;
}
}

bool ContextShim::UnregisterCrossContextObject(
const CrossContextMapInfo& info) {
auto i = crossContextObjects.find(info.object);
if (i != crossContextObjects.end()) {
std::vector<CrossContextMapInfo*>& v = i->second;
auto i2 = std::remove_if(v.begin(), v.end(),
[info](const CrossContextMapInfo* x) -> bool {
return x->toContext == info.toContext;
});

if (i2 != v.end()) {
v.erase(i2, v.end());
if (v.size() == 0) {
crossContextObjects.erase(i);
}

return true;
}
}

assert(false); // not found in map
return false;
}

bool ContextShim::TryGetCrossContextObject(JsValueRef object,
ContextShim* toContext,
JsValueRef* proxy) {
auto i = crossContextObjects.find(object);
if (i != crossContextObjects.end()) {
std::vector<CrossContextMapInfo*>& v = i->second;
auto i2 = std::find_if(v.begin(), v.end(),
[=](const CrossContextMapInfo* x) -> bool {
return x->toContext == toContext;
});

if (i2 != v.end()) {
*proxy = (*i2)->proxy;
return true;
}
}

return false;
}

void CALLBACK ContextShim::CrossContextFakeTargetFinalizeCallback(
void *callbackState) {
CrossContextMapInfo* info = static_cast<CrossContextMapInfo*>(callbackState);

ContextShim* fromContext = info->fromContext;
if (fromContext != nullptr) {
fromContext->UnregisterCrossContextObject(*info);
}

delete info;
}

void ContextShim::SetAlignedPointerInEmbedderData(int index, void * value) {
if (index < 0) {
return;
Expand Down Expand Up @@ -774,6 +665,30 @@ JsValueRef ContextShim::GetCreateTargetFunction() {
&createTargetFunction);
}


// This shim enables a builtin prototype function to support cross context
// objects. When "this" argument is cross context, marshal all arguments and
// make the call in "this" argument context. Otherwise delegate the call to
// cached function in current context.
JsValueRef CALLBACK PrototypeFunctionCrossContextShim(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState) {
ContextShim * originalContextShim = ContextShim::GetCurrent();
ContextShim::GlobalPrototypeFunction index =
*reinterpret_cast<ContextShim::GlobalPrototypeFunction*>(&callbackState);

JsValueRef function = originalContextShim->GetGlobalPrototypeFunction(index);
JsValueRef result;
if (JsCallFunction(function, arguments, argumentCount,
&result) != JsNoError) {
return JS_INVALID_REFERENCE;
}
return result;
}

} // namespace jsrt

namespace v8 {
Expand Down
30 changes: 6 additions & 24 deletions deps/chakrashim/src/jsrtcontextshim.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,28 +126,6 @@ class ContextShim {
bool CheckConfigGlobalObjectTemplate();
bool ExecuteChakraShimJS();

// Maintains a map of {(this fromContext) object -> (toContext) proxy} for
// cross context objects. We can return the same proxy when an object was
// previously marshalled to toContext.
struct CrossContextMapInfo {
ContextShim* fromContext;
ContextShim* toContext;
JsValueRef object; // original object
JsValueRef proxy; // result proxy
};
bool RegisterCrossContextObject(JsValueRef fakeTarget,
const CrossContextMapInfo& info);
bool UnregisterCrossContextObject(const CrossContextMapInfo& info);
bool TryGetCrossContextObject(JsValueRef object, ContextShim* toContext,
JsValueRef* proxy);
static void CALLBACK CrossContextFakeTargetFinalizeCallback(
void *callbackState);

friend JsValueRef MarshalObjectToContext(JsValueType valueType,
JsValueRef valueRef,
ContextShim * contextShim,
ContextShim * toContextShim);

IsolateShim * isolateShim;
JsContextRef context;
bool initialized;
Expand Down Expand Up @@ -192,8 +170,6 @@ class ContextShim {
JsValueRef createTargetFunction;

std::vector<void*> embedderData;
std::unordered_map<JsValueRef, std::vector<CrossContextMapInfo*>>
crossContextObjects;
};

template <class R>
Expand All @@ -209,4 +185,10 @@ R ContextShim::ExecuteInContextOf(
return fn();
}

JsValueRef CALLBACK PrototypeFunctionCrossContextShim(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);
} // namespace jsrt
Loading

0 comments on commit 8851bb6

Please sign in to comment.