Repository navigation
impl shadow realm as js builtins #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright (C) 2021 Phillip Mates <[email protected]>. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions | ||
| * are met: | ||
| * 1. Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * 2. Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | ||
| * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
| * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| @globalPrivate | ||
| function wrap(target) | ||
| { | ||
| "use strict"; | ||
|
|
||
| if (@isCallable(target)) { | ||
| let wrapped = (...args) => { | ||
| const wrappedArgs = args.map(@wrap); | ||
| const result = target.@apply(@undefined, wrappedArgs); | ||
| return @wrap(result); | ||
| }; | ||
| delete wrapped['name']; | ||
| delete wrapped['length']; | ||
| return wrapped; | ||
| } else if (@isObject(target)) { | ||
| @throwTypeError("value passing between realms must be callable or primitive"); | ||
| } | ||
| return target; | ||
| } | ||
|
|
||
| function evaluate(evalStr) | ||
| { | ||
| "use strict"; | ||
|
|
||
| let result = @evalInRealm(this, evalStr) | ||
| return @wrap(result); | ||
| } | ||
|
|
||
| function importValue(module, binding) | ||
| { | ||
| let bindingStr = binding.toString(); | ||
| let lookupBinding = (m) => { | ||
| if (bindingStr in m) { | ||
| return @wrap(m[bindingStr]); | ||
|
Comment on lines
+58
to
+59
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a better way to do this prop check/lookup? |
||
| } | ||
|
|
||
| @throwTypeError("import value doesn't exist"); | ||
| }; | ||
| return @importInRealm(this, module).then(lookupBinding); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,6 +183,9 @@ | |
| #include "ProxyConstructor.h" | ||
| #include "ProxyObject.h" | ||
| #include "ProxyRevoke.h" | ||
| #include "ShadowRealmConstructor.h" | ||
| #include "ShadowRealmObject.h" | ||
| #include "ShadowRealmPrototype.h" | ||
| #include "ReflectObject.h" | ||
| #include "RegExpConstructor.h" | ||
| #include "RegExpMatchesArray.h" | ||
|
|
@@ -914,6 +917,9 @@ void JSGlobalObject::init(VM& vm) | |
| for (unsigned i = 0; i < NumberOfArrayIndexingModes; ++i) | ||
| m_arrayStructureForIndexingShapeDuringAllocation[i] = m_originalArrayStructureForIndexingShape[i]; | ||
|
|
||
| m_shadowRealmPrototype.set(vm, this, ShadowRealmPrototype::create(vm, ShadowRealmPrototype::createStructure(vm, this, m_objectPrototype.get()))); | ||
| m_shadowRealmStructure.set(vm, this, ShadowRealmObject::createStructure(vm, this, m_shadowRealmPrototype.get())); | ||
|
|
||
| m_regExpPrototype.set(vm, this, RegExpPrototype::create(vm, this, RegExpPrototype::createStructure(vm, this, m_objectPrototype.get()))); | ||
| m_regExpStructure.set(vm, this, RegExpObject::createStructure(vm, this, m_regExpPrototype.get())); | ||
| m_regExpMatchesArrayStructure.set(vm, this, createRegExpMatchesArrayStructure(vm, this)); | ||
|
|
@@ -1018,7 +1024,11 @@ void JSGlobalObject::init(VM& vm) | |
| ArrayConstructor* arrayConstructor = ArrayConstructor::create(vm, this, ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_arrayPrototype.get(), m_speciesGetterSetter.get()); | ||
| m_arrayConstructor.set(vm, this, arrayConstructor); | ||
| m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::Array)].set(vm, this, arrayConstructor); | ||
|
|
||
|
|
||
| ShadowRealmConstructor* shadowRealmConstructor = ShadowRealmConstructor::create(vm, ShadowRealmConstructor::createStructure(vm, this, m_functionPrototype.get()), m_shadowRealmPrototype.get(), m_speciesGetterSetter.get()); | ||
| m_shadowRealmConstructor.set(vm, this, shadowRealmConstructor); | ||
| m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::ShadowRealm)].set(vm, this, shadowRealmConstructor); | ||
|
|
||
| RegExpConstructor* regExpConstructor = RegExpConstructor::create(vm, RegExpConstructor::createStructure(vm, this, m_functionPrototype.get()), m_regExpPrototype.get(), m_speciesGetterSetter.get()); | ||
| m_regExpConstructor.set(vm, this, regExpConstructor); | ||
| m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::RegExp)].set(vm, this, regExpConstructor); | ||
|
|
@@ -1098,6 +1108,7 @@ capitalName ## Constructor* lowerName ## Constructor = featureFlag ? capitalName | |
| m_functionPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, functionConstructor, static_cast<unsigned>(PropertyAttribute::DontEnum)); | ||
| m_arrayPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, arrayConstructor, static_cast<unsigned>(PropertyAttribute::DontEnum)); | ||
| m_regExpPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, regExpConstructor, static_cast<unsigned>(PropertyAttribute::DontEnum)); | ||
| m_shadowRealmPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, shadowRealmConstructor, static_cast<unsigned>(PropertyAttribute::DontEnum)); | ||
|
|
||
| putDirectWithoutTransition(vm, vm.propertyNames->Object, objectConstructor, static_cast<unsigned>(PropertyAttribute::DontEnum)); | ||
| putDirectWithoutTransition(vm, vm.propertyNames->Function, functionConstructor, static_cast<unsigned>(PropertyAttribute::DontEnum)); | ||
|
|
@@ -1242,6 +1253,7 @@ capitalName ## Constructor* lowerName ## Constructor = featureFlag ? capitalName | |
| TemporalObject* temporal = TemporalObject::create(vm, TemporalObject::createStructure(vm, this)); | ||
| putDirectWithoutTransition(vm, vm.propertyNames->Temporal, temporal, static_cast<unsigned>(PropertyAttribute::DontEnum)); | ||
| } | ||
| putDirectWithoutTransition(vm, vm.propertyNames->ShadowRealm, shadowRealmConstructor, static_cast<unsigned>(PropertyAttribute::DontEnum)); | ||
|
|
||
| m_moduleLoader.initLater( | ||
| [] (const Initializer<JSModuleLoader>& init) { | ||
|
|
@@ -1417,6 +1429,12 @@ capitalName ## Constructor* lowerName ## Constructor = featureFlag ? capitalName | |
| m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::thisTimeValue)].initLater([] (const Initializer<JSCell>& init) { | ||
| init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), dateProtoFuncGetTime, DatePrototypeGetTimeIntrinsic)); | ||
| }); | ||
| m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::importInRealm)].initLater([] (const Initializer<JSCell>& init) { | ||
| init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), importInRealm)); | ||
| }); | ||
| m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::evalInRealm)].initLater([] (const Initializer<JSCell>& init) { | ||
| init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), evalInRealm)); | ||
| }); | ||
| m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::sameValue)].initLater([] (const Initializer<JSCell>& init) { | ||
| init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 2, String(), objectConstructorIs, ObjectIsIntrinsic)); | ||
| }); | ||
|
|
@@ -2098,6 +2116,7 @@ void JSGlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) | |
| thisObject->m_URIErrorStructure.visit(visitor); | ||
| thisObject->m_aggregateErrorStructure.visit(visitor); | ||
| visitor.append(thisObject->m_arrayConstructor); | ||
| visitor.append(thisObject->m_shadowRealmConstructor); | ||
| visitor.append(thisObject->m_regExpConstructor); | ||
| visitor.append(thisObject->m_objectConstructor); | ||
| visitor.append(thisObject->m_functionConstructor); | ||
|
|
@@ -2194,6 +2213,7 @@ void JSGlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) | |
| thisObject->m_customSetterFunctionStructure.visit(visitor); | ||
| thisObject->m_boundFunctionStructure.visit(visitor); | ||
| thisObject->m_nativeStdFunctionStructure.visit(visitor); | ||
| visitor.append(thisObject->m_shadowRealmStructure); | ||
| visitor.append(thisObject->m_regExpStructure); | ||
| visitor.append(thisObject->m_generatorFunctionStructure); | ||
| visitor.append(thisObject->m_asyncFunctionStructure); | ||
|
|
@@ -2550,6 +2570,13 @@ JSGlobalObject* JSGlobalObject::create(VM& vm, Structure* structure) | |
| return globalObject; | ||
| } | ||
|
|
||
| JSGlobalObject* JSGlobalObject::create(VM& vm, Structure* structure, const GlobalObjectMethodTable* methodTable) | ||
| { | ||
| JSGlobalObject* globalObject = new (NotNull, allocateCell<JSGlobalObject>(vm.heap)) JSGlobalObject(vm, structure, methodTable); | ||
| globalObject->finishCreation(vm); | ||
| return globalObject; | ||
| } | ||
|
Comment on lines
+2573
to
+2578
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was the best way I could think of creating a new global object with a custom method table. We grab the method table from the original global object in https://github.com/WebKit/WebKit/pull/8/files#diff-44ebd0942590c3af4f75c8833b1b8f2dd9d649fd9e7dcc7a9e1410df5711c22fR33, which might be defined in |
||
|
|
||
| void JSGlobalObject::finishCreation(VM& vm) | ||
| { | ||
| DeferTermination deferTermination(vm); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #include "config.h" | ||
| #include "ShadowRealmConstructor.h" | ||
|
|
||
| #include "ShadowRealmObject.h" | ||
| #include "JSCInlines.h" | ||
|
|
||
|
|
||
| namespace JSC { | ||
|
|
||
| const ClassInfo ShadowRealmConstructor::s_info = { "Function", &InternalFunction::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(ShadowRealmConstructor) }; | ||
|
|
||
| STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ShadowRealmConstructor); | ||
|
|
||
| static JSC_DECLARE_HOST_FUNCTION(callShadowRealm); | ||
| static JSC_DECLARE_HOST_FUNCTION(constructWithShadowRealmConstructor); | ||
|
|
||
| ShadowRealmConstructor::ShadowRealmConstructor(VM& vm, Structure* structure) | ||
| : InternalFunction(vm, structure, callShadowRealm, constructWithShadowRealmConstructor) | ||
| { | ||
| } | ||
|
|
||
| void ShadowRealmConstructor::finishCreation(VM& vm, ShadowRealmPrototype* shadowRealmPrototype) | ||
| { | ||
| Base::finishCreation(vm, 0, vm.propertyNames->ShadowRealm.string(), PropertyAdditionMode::WithoutStructureTransition); | ||
| putDirectWithoutTransition(vm, vm.propertyNames->prototype, shadowRealmPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); | ||
| } | ||
|
|
||
| JSObject* constructShadowRealm(JSGlobalObject* globalObject, JSValue, const ArgList&) | ||
| { | ||
| VM& vm = globalObject->vm(); | ||
| auto scope = DECLARE_THROW_SCOPE(vm); | ||
| Structure* shadowRealmStructure = ShadowRealmObject::createStructure(vm, globalObject, globalObject->shadowRealmPrototype()); | ||
| return ShadowRealmObject::create(vm, shadowRealmStructure, globalObject->globalObjectMethodTable()); | ||
| } | ||
|
|
||
| JSC_DEFINE_HOST_FUNCTION(constructWithShadowRealmConstructor, (JSGlobalObject* globalObject, CallFrame* callFrame)) | ||
| { | ||
| ArgList args(callFrame); | ||
| return JSValue::encode(constructShadowRealm(globalObject, callFrame->newTarget(), args)); | ||
| } | ||
|
|
||
| JSC_DEFINE_HOST_FUNCTION(callShadowRealm, (JSGlobalObject* globalObject, CallFrame*)) | ||
| { | ||
| auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); | ||
| return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(globalObject, scope, "ShadowRealm")); | ||
| } | ||
|
|
||
| } // namespace JSC | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my (idiomatic) JS is horrible so in general be thorough with this file