Skip to content

Commit

Permalink
chakrashim: implement proxy methods in chakrashim
Browse files Browse the repository at this point in the history
Implement value::IsProxy(), Proxy::GetTarget() and Proxy::GetHandler()
in chakrashim. This enables util.types.isProxy() and util.inspect()
with the showProxy flag as true to function correctly in
node-chakracore.

Also enable related tests for node-chakracore:
* parallel/test-util-types.js
* parallel/test-util-inspect.js

Fixes: nodejs#488
Fixes: nodejs#523
PR-URL: nodejs#525
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Reviewed-By: Jack Horton <Jack.Horton@microsoft.com>
  • Loading branch information
rhuanjl authored and kfarnung committed May 10, 2018
1 parent 2cd0485 commit e04616c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
5 changes: 0 additions & 5 deletions deps/chakrashim/lib/chakra_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,6 @@
return microTasks.shift();
};

utils.isProxy = function(value) {
// CHAKRA-TODO: Need to add JSRT API to detect this
return false;
};

utils.getPropertyAttributes = function(object, value) {
const descriptor = Object_getOwnPropertyDescriptor(object, value);
if (descriptor === undefined) {
Expand Down
20 changes: 14 additions & 6 deletions deps/chakrashim/src/v8proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@

namespace v8 {
Local<Object> Proxy::GetTarget() {
// CHAKRA-TODO: Need to add JSRT API to get target of proxy
JsValueRef undefinedValue = jsrt::GetUndefined();
return Local<Object>::New(undefinedValue);
JsValueRef returnValue = JS_INVALID_REFERENCE;
bool isProxy = false;
JsGetProxyProperties((JsValueRef) this, &isProxy, &returnValue, nullptr);
if (!isProxy || returnValue == JS_INVALID_REFERENCE) {
returnValue = jsrt::GetUndefined();
}
return Local<Object>::New(returnValue);
}

Local<Value> Proxy::GetHandler() {
// CHAKRA-TODO: Need to add JSRT API to get handler of proxy
JsValueRef undefinedValue = jsrt::GetUndefined();
return Local<Value>::New(undefinedValue);
JsValueRef returnValue = JS_INVALID_REFERENCE;
bool isProxy = false;
JsGetProxyProperties((JsValueRef) this, &isProxy, nullptr, &returnValue);
if (!isProxy || returnValue == JS_INVALID_REFERENCE) {
returnValue = jsrt::GetUndefined();
}
return Local<Object>::New(returnValue);
}

Proxy *Proxy::Cast(v8::Value *obj) {
Expand Down
7 changes: 6 additions & 1 deletion deps/chakrashim/src/v8value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ bool Value::IsUint32() const {
return trunc(value) == value;
}

bool Value::IsProxy() const {
bool isProxy = false;
JsGetProxyProperties((JsValueRef) this, &isProxy, nullptr, nullptr);
return isProxy;
}

#define IS_TYPE_FUNCTION(v8ValueFunc, chakrashimFunc) \
bool Value::v8ValueFunc() const { \
JsValueRef resultRef = JS_INVALID_REFERENCE; \
Expand All @@ -163,7 +169,6 @@ IS_TYPE_FUNCTION(IsDate, isDate)
IS_TYPE_FUNCTION(IsMap, isMap)
IS_TYPE_FUNCTION(IsNativeError, isNativeError)
IS_TYPE_FUNCTION(IsPromise, isPromise)
IS_TYPE_FUNCTION(IsProxy, isProxy)
IS_TYPE_FUNCTION(IsRegExp, isRegExp)
IS_TYPE_FUNCTION(IsAsyncFunction, isAsyncFunction)
IS_TYPE_FUNCTION(IsSet, isSet)
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/parallel.status
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ test-trace-events-v8 : SKIP
test-url-domain-ascii-unicode : SKIP
test-util : SKIP
test-util-format-shared-arraybuffer : SKIP
test-util-inspect-proxy : SKIP
test-v8-serdes : SKIP
test-vm-attributes-property-not-on-sandbox : SKIP
test-vm-cached-data : SKIP
Expand Down Expand Up @@ -156,7 +155,6 @@ test-vm-module-errors : SKIP
test-vm-module-import-meta : SKIP
test-vm-module-link : SKIP
test-vm-module-reevaluate : SKIP
test-util-types : SKIP

# This test requires support for bigints
test-util-inspect-bigint : SKIP
Expand Down
21 changes: 11 additions & 10 deletions test/parallel/test-util-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ for (const [ value, _method ] of [
{ value: 'foo' }) ],
[ new DataView(new ArrayBuffer()) ],
[ new SharedArrayBuffer() ],
// Node-ChakraCore does not support util.types.isProxy() due to
// Microsoft/ChakraCore#950 and node/node-chakracore#488
!common.isChakraEngine ? [ new Proxy({}, {}), 'isProxy' ] : [ new Date() ],
[ new Proxy({}, {}), 'isProxy' ],
[ new WebAssembly.Module(wasmBuffer), 'isWebAssemblyCompiledModule' ],
]) {
const method = _method || `is${value.constructor.name}`;
Expand Down Expand Up @@ -130,10 +128,13 @@ for (const [ value, _method ] of [
}
}

(async () => {
const m = new vm.Module('');
await m.link(() => 0);
m.instantiate();
await m.evaluate();
assert.ok(types.isModuleNamespaceObject(m.namespace));
})();
// Node-ChakraCore does not support esmodules
if (!common.isChakraEngine) {
(async () => {
const m = new vm.Module('');
await m.link(() => 0);
m.instantiate();
await m.evaluate();
assert.ok(types.isModuleNamespaceObject(m.namespace));
})();
}

0 comments on commit e04616c

Please sign in to comment.