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

Commit

Permalink
chakrashim: align Local/Persistent with official v8.h
Browse files Browse the repository at this point in the history
Update Local<> and Persistent<> definitions to align with
official v8.h. They no longer share a common base class.

Refactor v8::chakrashim::InternalMethods to v8::Utils.

Remove PrototypeFunctionCrossContextShim and now unused prototype methods
cache.

Reviewed-by: @kunalspathak
  • Loading branch information
Jianchun Xu committed Aug 18, 2015
1 parent 39685df commit b461501
Show file tree
Hide file tree
Showing 30 changed files with 665 additions and 750 deletions.
761 changes: 355 additions & 406 deletions deps/chakrashim/include/v8.h

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions deps/chakrashim/src/jsrtcontextcachedobj.inc
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,8 @@ DEFTYPE(URIError)


// These prototype functions will be cached/shimmed
DEFMETHOD(ArrayBuffer, toString)
DEFMETHOD(ArrayBuffer, slice)
DEFMETHOD(Boolean, toString)
DEFMETHOD(Date, toString)
DEFMETHOD(Function, toString)
DEFMETHOD(Number, toString)
DEFMETHOD(Object, toString)
DEFMETHOD(RegExp, toString)
DEFMETHOD(String, concat)
DEFMETHOD(String, toString)

#undef DEFTYPE
#undef DEFMETHOD
95 changes: 34 additions & 61 deletions deps/chakrashim/src/jsrtcontextshim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ bool ContextShim::InitializeGlobalPrototypeFunctions() {
GlobalType type, JsPropertyIdRef functionIdRef) {
// Cache the builtin function on the type's prototype
JsValueRef prototype;
if (!InitializeBuiltIn(&globalPrototypeFunction[index],
return InitializeBuiltIn(&globalPrototypeFunction[index],
[=, &prototype](JsValueRef * value) {
JsErrorCode error = JsGetProperty(
globalConstructor[type], prototypeIdRef, &prototype);
Expand All @@ -194,19 +194,7 @@ bool ContextShim::InitializeGlobalPrototypeFunctions() {
}

return JsGetProperty(prototype, functionIdRef, value);
})) {
return false;
}

// Replace the builtin function with a cross context shim function
JsValueRef function;
if (JsCreateFunction(jsrt::PrototypeFunctionCrossContextShim,
reinterpret_cast<void*>(index),
&function) != JsNoError) {
return false;
}
return JsSetProperty(prototype, functionIdRef, function,
false) == JsNoError;
});
};

struct TypeMethodPair {
Expand Down Expand Up @@ -234,18 +222,26 @@ bool ContextShim::InitializeGlobalPrototypeFunctions() {
// Replace (cached) Object.prototype.toString with a shim to support
// ObjectTemplate class name. Called after InitializeGlobalPrototypeFunctions().
bool ContextShim::InitializeObjectPrototypeToStringShim() {
IsolateShim* iso = GetIsolateShim();

JsValueRef objectPrototype;
if (JsGetProperty(GetObjectConstructor(),
iso->GetCachedPropertyIdRef(CachedPropertyIdRef::prototype),
&objectPrototype) != JsNoError) {
return false;
}

JsValueRef function;
if (!InitializeBuiltIn(&function, [=](JsValueRef * value) {
JsErrorCode error = JsCreateFunction(
v8::chakrashim::InternalMethods::ObjectPrototypeToStringShim,
GetGlobalPrototypeFunction(GlobalPrototypeFunction::Object_toString),
value);
if (error != JsNoError) {
return error;
}
if (JsCreateFunction(v8::Utils::ObjectPrototypeToStringShim,
GetGlobalPrototypeFunction(
GlobalPrototypeFunction::Object_toString),
&function) != JsNoError) {
return false;
}

return JsNoError;
})) {
if (JsSetProperty(objectPrototype,
iso->GetCachedPropertyIdRef(CachedPropertyIdRef::toString),
function, false) != JsNoError) {
return false;
}

Expand Down Expand Up @@ -665,54 +661,32 @@ 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 {
namespace chakrashim {

// This shim wraps Object.prototype.toString to supports ObjectTemplate class
// name.
JsValueRef CALLBACK InternalMethods::ObjectPrototypeToStringShim(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState) {
JsValueRef CALLBACK Utils::ObjectPrototypeToStringShim(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState) {
if (argumentCount >= 1) {
using namespace v8;
Isolate* iso = Isolate::GetCurrent();
HandleScope scope(iso);

Object* obj = static_cast<Object*>(arguments[0]);
Local<String> str = InternalMethods::GetClassName(obj);
if (!str.IsEmpty()) {
str = String::Concat(String::NewFromUtf8(iso, "[object "), str);
str = String::Concat(str, String::NewFromUtf8(iso, "]"));
return *str;
ObjectTemplate* objTemplate = obj->GetObjectTemplate();
if (objTemplate) {
Local<String> str = objTemplate->GetClassName();
if (!str.IsEmpty()) {
str = String::Concat(String::NewFromUtf8(iso, "[object "), str);
str = String::Concat(str, String::NewFromUtf8(iso, "]"));
return *str;
}
}
}

Expand All @@ -725,5 +699,4 @@ JsValueRef CALLBACK InternalMethods::ObjectPrototypeToStringShim(
return result;
}

} // namespace chakrashim
} // namespace v8
9 changes: 0 additions & 9 deletions deps/chakrashim/src/jsrtcontextshim.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ class ContextShim {

static ContextShim * GetCurrent();

template <class R>
static R ExecuteInContextOf(JsValueRef object, const std::function<R()> & fn);

private:
ContextShim(IsolateShim * isolateShim, JsContextRef context, bool exposeGC,
JsValueRef globalObjectTemplateInstance);
Expand Down Expand Up @@ -172,10 +169,4 @@ class ContextShim {
std::vector<void*> embedderData;
};

JsValueRef CALLBACK PrototypeFunctionCrossContextShim(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);
} // namespace jsrt
8 changes: 4 additions & 4 deletions deps/chakrashim/src/jsrtpromise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

namespace jsrt {

static void CALLBACK PromiseContinuationCallback(
JsValueRef task, void *callbackState) {
static void CALLBACK PromiseContinuationCallback(JsValueRef task,
void *callbackState) {
// CHAKRA-REVIEW: We have a current context here?
JsValueRef promiseContinuationFunction =
jsrt::ContextShim::GetCurrent()->GetPromiseContinuationFunction();
ContextShim::GetCurrent()->GetPromiseContinuationFunction();

JsValueRef args[] = { *v8::Undefined(), task };
JsValueRef args[] = { nullptr, task };
JsValueRef result;
JsCallFunction(promiseContinuationFunction, args, _countof(args), &result);
}
Expand Down
1 change: 0 additions & 1 deletion deps/chakrashim/src/jsrtutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#pragma once

#include "jsrt.h"
#include "v8.h"
#include "jsrtproxyutils.h"
#include "jsrtcontextshim.h"
Expand Down
5 changes: 2 additions & 3 deletions deps/chakrashim/src/v8array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

#include "v8.h"
#include "jsrt.h"
#include "v8chakra.h"
#include "jsrtutils.h"

namespace v8 {
Expand All @@ -37,7 +36,7 @@ Local<Array> Array::New(Isolate* isolate, int length) {
return Local<Array>();
}

return Local<Array>::New(static_cast<Array*>(newArrayRef));
return Local<Array>::New(newArrayRef);
}

Array *Array::Cast(Value *obj) {
Expand Down
4 changes: 2 additions & 2 deletions deps/chakrashim/src/v8arraybuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Local<ArrayBuffer> ArrayBuffer::New(Isolate* isolate, size_t byte_length) {
&result) != JsNoError) {
return Local<ArrayBuffer>();
}
return static_cast<ArrayBuffer*>(result);
return Local<ArrayBuffer>::New(result);
}

struct ArrayBufferFinalizeInfo {
Expand Down Expand Up @@ -75,7 +75,7 @@ Local<ArrayBuffer> ArrayBuffer::New(Isolate* isolate,
}
return Local<ArrayBuffer>();
}
return static_cast<ArrayBuffer*>(result);
return Local<ArrayBuffer>::New(result);
}

size_t ArrayBuffer::ByteLength() const {
Expand Down
4 changes: 2 additions & 2 deletions deps/chakrashim/src/v8boolean.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ bool Boolean::Value() const {

Handle<Boolean> Boolean::New(Isolate* isolate, bool value) {
if (value) {
return True();
return True(isolate);
} else {
return False();
return False(isolate);
}
}

Expand Down
5 changes: 2 additions & 3 deletions deps/chakrashim/src/v8booleanobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

#include "v8.h"
#include "v8chakra.h"
#include "jsrtutils.h"

namespace v8 {
Expand All @@ -39,8 +39,7 @@ Local<Value> BooleanObject::New(bool value) {
return Local<Value>();
}

return Local<BooleanObject>::New(
static_cast<BooleanObject*>(newBooleanObjectRef));
return Local<BooleanObject>::New(newBooleanObjectRef);
}

BooleanObject *BooleanObject::Cast(v8::Value *obj) {
Expand Down
95 changes: 79 additions & 16 deletions deps/chakrashim/src/v8chakra.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.


// Internal header for the v8-chakra bridge

#pragma once
#include "v8.h"
#include <assert.h>
#include "jsrtutils.h"

namespace v8 {

Expand Down Expand Up @@ -64,31 +61,97 @@ struct TemplateData {
Object* EnsureProperties();
};

namespace chakrashim {

class InternalMethods {
class Utils {
public:
static Handle<String> GetClassName(ObjectTemplate* objectTemplate);
static JsValueRef CALLBACK AccessorHandler(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);

static JsValueRef CALLBACK ObjectPrototypeToStringShim(
static JsValueRef CALLBACK GetCallback(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);
static JsValueRef CALLBACK SetCallback(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);
static JsValueRef CALLBACK DeletePropertyCallback(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);

static Handle<String> GetClassName(Object* obj) {
return GetClassName(obj->GetObjectTemplate());
}
static JsValueRef HasPropertyHandler(
JsValueRef *arguments,
unsigned short argumentCount,
bool checkInPrototype);
static JsValueRef CALLBACK HasCallback(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);

static JsValueRef GetPropertiesEnumeratorHandler(
JsValueRef* arguments,
unsigned int argumentsCount);
static JsValueRef CALLBACK EnumerateCallback(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);

static JsValueRef GetPropertiesHandler(
JsValueRef* arguments,
unsigned int argumentsCount,
bool getFromPrototype);
static JsValueRef CALLBACK OwnKeysCallback(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);
static JsValueRef CALLBACK GetOwnPropertyDescriptorCallback(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);

static void CALLBACK WeakReferenceCallbackWrapperCallback(
JsRef ref, void *data);

static JsValueRef CALLBACK ObjectPrototypeToStringShim(
JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
unsigned short argumentCount,
void *callbackState);

static bool IsInstanceOf(Object* obj, ObjectTemplate* objectTemplate) {
return obj->GetObjectTemplate() == objectTemplate;
}
};

bool CheckSignature(Local<FunctionTemplate> receiver, Local<Object> thisPointer,
Local<Object>* holder);
static bool CheckSignature(Local<FunctionTemplate> receiver,
Local<Object> thisPointer,
Local<Object>* holder);

template <class Func>
static Local<Value> NewError(Handle<String> message, const Func& f);

} // namespace chakrashim
template <class T>
static Local<T> NewTypedArray(jsrt::ContextShim::GlobalType constructorIndex,
Handle<ArrayBuffer> array_buffer,
size_t byte_offset, size_t length);
};

} // namespace v8
Loading

0 comments on commit b461501

Please sign in to comment.