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

Commit

Permalink
chakrashim: Fixed breaking v8 APIs after merge
Browse files Browse the repository at this point in the history
Implemented `Private` that is used as private symbol to store on object.
* GetPrivate
* SetPrivate
* HasPrivate
* DeletePrivate

No new unittest failures.

Reviewed-By: @jianchun
  • Loading branch information
kunalspathak committed Feb 9, 2016
1 parent 53bfef3 commit ee02cb6
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 117 deletions.
1 change: 1 addition & 0 deletions deps/chakrashim/chakrashim.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
'src/v8object.cc',
'src/v8objecttemplate.cc',
'src/v8persistent.cc',
'src/v8private.cc',
'src/v8script.cc',
'src/v8signature.cc',
'src/v8stacktrace.cc',
Expand Down
25 changes: 23 additions & 2 deletions deps/chakrashim/include/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class Local {
friend class Object;
friend struct ObjectData;
friend class ObjectTemplate;
friend class Private;
friend class Signature;
friend class Script;
friend class StackFrame;
Expand Down Expand Up @@ -1039,6 +1040,17 @@ class V8_EXPORT Value : public Data {
}
};

class V8_EXPORT Private : public Data {
public:
Local<Value> Name() const;
static Local<Private> New(Isolate* isolate,
Local<String> name = Local<String>());
static Local<Private> ForApi(Isolate* isolate, Local<String> name);

private:
Private();
};

class V8_EXPORT Primitive : public Value {
public:
};
Expand Down Expand Up @@ -1380,8 +1392,17 @@ class V8_EXPORT Object : public Value {
V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
Local<Context> context, Local<Name> key);

bool SetHiddenValue(Handle<String> key, Handle<Value> value);
Local<Value> GetHiddenValue(Handle<String> key);
V8_DEPRECATE_SOON("Use v8::Object::SetPrivate instead.",
bool SetHiddenValue(Handle<String> key,
Handle<Value> value));
V8_DEPRECATE_SOON("Use v8::Object::GetPrivate instead.",
Local<Value> GetHiddenValue(Handle<String> key));

Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key);
Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key,
Local<Value> value);
Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key);
MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key);

Local<Object> Clone();
Local<Context> CreationContext();
Expand Down
11 changes: 9 additions & 2 deletions deps/chakrashim/lib/chakra_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
Map_values = Map.prototype.values,
Map_entries = Map.prototype.entries,
Set_entries = Set.prototype.entries,
Set_values = Set.prototype.values;
Set_values = Set.prototype.values,
Symbol_keyFor = Symbol.keyFor,
Symbol_for = Symbol.for;

// Simulate V8 JavaScript stack trace API
function StackFrame(funcName, fileName, lineNumber, columnNumber) {
Expand Down Expand Up @@ -350,7 +352,12 @@
utils.isNumberObject = function(obj) {
return compareType(obj, 'Number');
};

utils.getSymbolKeyFor = function(symbol) {
return Symbol_keyFor(symbol);
};
utils.getSymbolFor = function(key) {
return Symbol_for(key);
};
}

// patch console
Expand Down
2 changes: 2 additions & 0 deletions deps/chakrashim/src/jsrtcachedpropertyidref.inc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ DEF(createPropertyDescriptorsEnumerationIterator)
DEF(getNamedOwnKeys)
DEF(getIndexedOwnKeys)
DEF(getStackTrace)
DEF(getSymbolKeyFor)
DEF(getSymbolFor)
DEF(getFunctionName)
DEF(getFileName)
DEF(getColumnNumber)
Expand Down
9 changes: 7 additions & 2 deletions deps/chakrashim/src/jsrtcontextshim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ ContextShim::ContextShim(IsolateShim * isolateShim,
getEnumerableNamedPropertiesFunction(JS_INVALID_REFERENCE),
getEnumerableIndexedPropertiesFunction(JS_INVALID_REFERENCE),
createEnumerationIteratorFunction(JS_INVALID_REFERENCE),
createPropertyDescriptorsEnumerationIteratorFunction(JS_INVALID_REFERENCE),
createPropertyDescriptorsEnumerationIteratorFunction
(JS_INVALID_REFERENCE),
getNamedOwnKeysFunction(JS_INVALID_REFERENCE),
getIndexedOwnKeysFunction(JS_INVALID_REFERENCE),
getStackTraceFunction(JS_INVALID_REFERENCE) {
getStackTraceFunction(JS_INVALID_REFERENCE),
getSymbolKeyForFunction(JS_INVALID_REFERENCE),
getSymbolForFunction(JS_INVALID_REFERENCE) {
memset(globalConstructor, 0, sizeof(globalConstructor));
memset(globalPrototypeFunction, 0, sizeof(globalPrototypeFunction));
}
Expand Down Expand Up @@ -569,6 +572,8 @@ CHAKRASHIM_FUNCTION_GETTER(createPropertyDescriptorsEnumerationIterator)
CHAKRASHIM_FUNCTION_GETTER(getNamedOwnKeys)
CHAKRASHIM_FUNCTION_GETTER(getIndexedOwnKeys)
CHAKRASHIM_FUNCTION_GETTER(getStackTrace)
CHAKRASHIM_FUNCTION_GETTER(getSymbolKeyFor)
CHAKRASHIM_FUNCTION_GETTER(getSymbolFor)

#define DEF_IS_TYPE(F) CHAKRASHIM_FUNCTION_GETTER(F)
#include "jsrtcachedpropertyidref.inc"
Expand Down
2 changes: 2 additions & 0 deletions deps/chakrashim/src/jsrtcontextshim.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ JsValueRef F##Function; \
DECLARE_CHAKRASHIM_FUNCTION_GETTER(getNamedOwnKeys);
DECLARE_CHAKRASHIM_FUNCTION_GETTER(getIndexedOwnKeys);
DECLARE_CHAKRASHIM_FUNCTION_GETTER(getStackTrace);
DECLARE_CHAKRASHIM_FUNCTION_GETTER(getSymbolKeyFor);
DECLARE_CHAKRASHIM_FUNCTION_GETTER(getSymbolFor);

#define DEF_IS_TYPE(F) DECLARE_CHAKRASHIM_FUNCTION_GETTER(F)
#include "jsrtcachedpropertyidref.inc"
Expand Down
147 changes: 147 additions & 0 deletions deps/chakrashim/src/jsrtutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,153 @@ JsErrorCode ParseScript(const wchar_t *script,
}
}

#define RETURN_IF_JSERROR(err, returnValue) \
if (err != JsNoError) { \
return returnValue; \
}

JsErrorCode GetHiddenValuesTable(JsValueRef object,
JsPropertyIdRef* hiddenValueIdRef,
JsValueRef* hiddenValuesTable,
bool* isUndefined) {
*isUndefined = true;
IsolateShim* iso = IsolateShim::GetCurrent();
*hiddenValueIdRef = iso->GetCachedSymbolPropertyIdRef(
CachedSymbolPropertyIdRef::__hiddenvalues__);
JsErrorCode errorCode;

errorCode = JsGetProperty(object, *hiddenValueIdRef, hiddenValuesTable);
RETURN_IF_JSERROR(errorCode, errorCode);

errorCode = IsUndefined(*hiddenValuesTable, isUndefined);
RETURN_IF_JSERROR(errorCode, errorCode);

return JsNoError;
}

bool HasPrivate(JsValueRef object, JsValueRef key) {
JsPropertyIdRef hiddenValuesIdRef;
JsValueRef hiddenValuesTable;
JsErrorCode errorCode;
bool isUndefined;

errorCode = GetHiddenValuesTable(object, &hiddenValuesIdRef,
&hiddenValuesTable, &isUndefined);
RETURN_IF_JSERROR(errorCode, false);

if (isUndefined) {
return false;
}

JsValueRef hasPropertyRef;
errorCode = jsrt::HasOwnProperty(hiddenValuesTable, key, &hasPropertyRef);
RETURN_IF_JSERROR(errorCode, false);

bool hasKey;
errorCode = JsBooleanToBool(hasPropertyRef, &hasKey);
RETURN_IF_JSERROR(errorCode, false);

return hasKey;
}

bool DeletePrivate(JsValueRef object, JsValueRef key) {
JsPropertyIdRef hiddenValuesIdRef;
JsValueRef hiddenValuesTable;
JsErrorCode errorCode;
bool isUndefined;

errorCode = GetHiddenValuesTable(object, &hiddenValuesIdRef,
&hiddenValuesTable, &isUndefined);
RETURN_IF_JSERROR(errorCode, false);

if (isUndefined) {
return false;
}

JsValueRef deleteResultRef;
errorCode = jsrt::DeleteProperty(hiddenValuesTable, key, &deleteResultRef);
RETURN_IF_JSERROR(errorCode, false);

bool hasDeleted;
errorCode = JsBooleanToBool(deleteResultRef, &hasDeleted);
RETURN_IF_JSERROR(errorCode, false);

return hasDeleted;
}

JsErrorCode GetPrivate(JsValueRef object, JsValueRef key,
JsValueRef *result) {
JsPropertyIdRef hiddenValuesIdRef;
JsValueRef hiddenValuesTable;
JsErrorCode errorCode;
JsValueRef undefinedValueRef = GetUndefined();
bool isUndefined;

errorCode = GetHiddenValuesTable(object, &hiddenValuesIdRef,
&hiddenValuesTable, &isUndefined);
RETURN_IF_JSERROR(errorCode, errorCode);

if (isUndefined) {
*result = undefinedValueRef;
return JsNoError;
}

JsPropertyIdRef keyIdRef;
errorCode = GetPropertyIdFromName(key, &keyIdRef);
RETURN_IF_JSERROR(errorCode, errorCode);

// Is 'key' present in hiddenValuesTable? If not, return undefined
JsValueRef hasPropertyRef;
errorCode = HasOwnProperty(hiddenValuesTable, key, &hasPropertyRef);
RETURN_IF_JSERROR(errorCode, errorCode);

bool hasKey;
errorCode = JsBooleanToBool(hasPropertyRef, &hasKey);
RETURN_IF_JSERROR(errorCode, errorCode);

if (!hasKey) {
*result = undefinedValueRef;
return JsNoError;
}

errorCode = JsGetProperty(hiddenValuesTable, keyIdRef, result);
RETURN_IF_JSERROR(errorCode, errorCode);

return JsNoError;
}

JsErrorCode SetPrivate(JsValueRef object, JsValueRef key,
JsValueRef value) {
JsPropertyIdRef hiddenValuesIdRef;
JsValueRef hiddenValuesTable;
JsErrorCode errorCode;
bool isUndefined;

errorCode = GetHiddenValuesTable(object, &hiddenValuesIdRef,
&hiddenValuesTable, &isUndefined);
RETURN_IF_JSERROR(errorCode, errorCode);

// if '__hiddenvalues__' is not defined on object, define it
if (isUndefined) {
errorCode = JsCreateObject(&hiddenValuesTable);
RETURN_IF_JSERROR(errorCode, errorCode);

errorCode = DefineProperty(object, hiddenValuesIdRef,
PropertyDescriptorOptionValues::False,
PropertyDescriptorOptionValues::False,
PropertyDescriptorOptionValues::False,
hiddenValuesTable,
JS_INVALID_REFERENCE,
JS_INVALID_REFERENCE);
RETURN_IF_JSERROR(errorCode, errorCode);
}

errorCode = jsrt::SetProperty(hiddenValuesTable, key, value);
RETURN_IF_JSERROR(errorCode, errorCode);

return JsNoError;
}

void Unimplemented(const char * message) {
fprintf(stderr, "FATAL ERROR: '%s' unimplemented", message);
__debugbreak();
Expand Down
15 changes: 15 additions & 0 deletions deps/chakrashim/src/jsrtutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ JsErrorCode ParseScript(const wchar_t *script,
bool isStrictMode,
JsValueRef *result);

JsErrorCode GetHiddenValuesTable(JsValueRef object,
JsPropertyIdRef* hiddenValueIdRef,
JsValueRef* hiddenValuesTable,
bool* isUndefined);

JsErrorCode GetPrivate(JsValueRef object, JsValueRef key,
JsValueRef *result);

JsErrorCode SetPrivate(JsValueRef object, JsValueRef key,
JsValueRef value);

bool HasPrivate(JsValueRef object, JsValueRef key);

bool DeletePrivate(JsValueRef object, JsValueRef key);

void Unimplemented(const char * message);

void Fatal(const char * format, ...);
Expand Down
Loading

0 comments on commit ee02cb6

Please sign in to comment.