Skip to content
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

Update public API for using NewTarget from outside of escargot #1242

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions src/api/EscargotPublic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,17 @@ ObjectRef* ObjectRef::create(ExecutionStateRef* state)
#endif
}

ObjectRef* ObjectRef::create(ExecutionStateRef* state, ObjectRef* proto)
{
#if defined(ESCARGOT_SMALL_CONFIG)
auto obj = new Object(*toImpl(state), toImpl(proto));
obj->markThisObjectDontNeedStructureTransitionTable();
return toRef(obj);
#else
return toRef(new Object(*toImpl(state), toImpl(proto)));
#endif
}


// can not redefine or delete virtual property
class ExposableObject : public DerivedObject {
Expand Down Expand Up @@ -2658,6 +2669,16 @@ class CallPublicFunctionData : public gc {
FunctionObjectRef::NativeFunctionPointer m_publicFn;
};

class CallPublicFunctionWithNewTargetData : public gc {
public:
CallPublicFunctionWithNewTargetData(FunctionObjectRef::NativeFunctionWithNewTargetPointer publicFn)
: m_publicFn(publicFn)
{
}

FunctionObjectRef::NativeFunctionWithNewTargetPointer m_publicFn;
};

static Value publicFunctionBridge(ExecutionState& state, Value thisValue, size_t calledArgc, Value* calledArgv, Optional<Object*> newTarget)
{
ExtendedNativeFunctionObject* func = state.resolveCallee()->asExtendedNativeFunctionObject();
Expand All @@ -2671,6 +2692,24 @@ static Value publicFunctionBridge(ExecutionState& state, Value thisValue, size_t
return toImpl(code->m_publicFn(toRef(&state), toRef(thisValue), calledArgc, newArgv, newTarget.hasValue()));
}

static Value publicFunctionBridgeWithNewTarget(ExecutionState& state, Value thisValue, size_t calledArgc, Value* calledArgv, Optional<Object*> newTarget)
{
ExtendedNativeFunctionObject* func = state.resolveCallee()->asExtendedNativeFunctionObject();
CallPublicFunctionWithNewTargetData* code = func->internalSlotAsPointer<CallPublicFunctionWithNewTargetData>(FunctionObjectRef::BuiltinFunctionSlot::PublicFunctionIndex);

ValueRef** newArgv = ALLOCA(sizeof(ValueRef*) * calledArgc, ValueRef*);
for (size_t i = 0; i < calledArgc; i++) {
newArgv[i] = toRef(calledArgv[i]);
}

OptionalRef<ObjectRef> newTargetRef;
if (newTarget) {
newTargetRef = toRef(newTarget.value());
}

return toImpl(code->m_publicFn(toRef(&state), toRef(thisValue), calledArgc, newArgv, newTargetRef));
}

typedef void (*SecurityCheckCallback)(ExecutionStateRef* state, GlobalObjectProxyObjectRef* proxy, GlobalObjectRef* targetGlobalObject);

GlobalObjectProxyObjectRef* GlobalObjectProxyObjectRef::create(ExecutionStateRef* state, GlobalObjectRef* target, SecurityCheckCallback callback)
Expand All @@ -2693,16 +2732,28 @@ static FunctionObjectRef* createFunction(ExecutionStateRef* state, FunctionObjec
int flags = 0;
flags |= info.m_isStrict ? NativeFunctionInfo::Strict : 0;
flags |= info.m_isConstructor ? NativeFunctionInfo::Constructor : 0;
NativeFunctionInfo nativeInfo(toImpl(info.m_name), publicFunctionBridge, info.m_argumentCount, flags);
NativeFunctionInfo nativeInfo(toImpl(info.m_name), nullptr, info.m_argumentCount, flags);

if (info.m_hasWithNewTargetCallback) {
nativeInfo.m_nativeFunction = publicFunctionBridgeWithNewTarget;
} else {
nativeInfo.m_nativeFunction = publicFunctionBridge;
}

ExtendedNativeFunctionObject* func;
if (isBuiltin)
if (isBuiltin) {
func = new ExtendedNativeFunctionObjectImpl<1>(*toImpl(state), nativeInfo, NativeFunctionObject::__ForBuiltinConstructor__);
else
} else {
func = new ExtendedNativeFunctionObjectImpl<1>(*toImpl(state), nativeInfo);
}

CallPublicFunctionData* data = new CallPublicFunctionData(info.m_nativeFunction);
func->setInternalSlotAsPointer(FunctionObjectRef::BuiltinFunctionSlot::PublicFunctionIndex, data);
if (info.m_hasWithNewTargetCallback) {
CallPublicFunctionWithNewTargetData* data = new CallPublicFunctionWithNewTargetData(info.m_nativeFunctionWithNewTarget);
func->setInternalSlotAsPointer(FunctionObjectRef::BuiltinFunctionSlot::PublicFunctionIndex, data);
} else {
CallPublicFunctionData* data = new CallPublicFunctionData(info.m_nativeFunction);
func->setInternalSlotAsPointer(FunctionObjectRef::BuiltinFunctionSlot::PublicFunctionIndex, data);
}

return toRef(func);
}
Expand Down
21 changes: 20 additions & 1 deletion src/api/EscargotPublic.h
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,7 @@ typedef bool (*ExposableObjectDeleteOwnPropertyCallback)(ExecutionStateRef* stat
class ESCARGOT_EXPORT ObjectRef : public PointerValueRef {
public:
static ObjectRef* create(ExecutionStateRef* state);
static ObjectRef* create(ExecutionStateRef* state, ObjectRef* proto);
// can not redefine or delete virtual property
// virtual property does not follow every rule of ECMAScript
static ObjectRef* createExposableObject(ExecutionStateRef* state,
Expand Down Expand Up @@ -1495,7 +1496,10 @@ class ESCARGOT_EXPORT FunctionObjectRef : public ObjectRef {
public:
// if newTarget is present, that means constructor call
// in constructor call, function must return newly created object && thisValue is always undefined
typedef ValueRef* (*NativeFunctionPointer)(ExecutionStateRef* state, ValueRef* thisValue, size_t argc, ValueRef** argv, bool isConstructorCall);
typedef ValueRef* (*NativeFunctionPointer)(ExecutionStateRef* state, ValueRef* thisValue,
size_t argc, ValueRef** argv, bool isConstructorCall);
typedef ValueRef* (*NativeFunctionWithNewTargetPointer)(ExecutionStateRef* state, ValueRef* thisValue,
size_t argc, ValueRef** argv, OptionalRef<ObjectRef> newTarget);

enum BuiltinFunctionSlot : size_t {
PublicFunctionIndex = 0,
Expand All @@ -1504,15 +1508,30 @@ class ESCARGOT_EXPORT FunctionObjectRef : public ObjectRef {
struct ESCARGOT_EXPORT NativeFunctionInfo {
bool m_isStrict;
bool m_isConstructor;
bool m_hasWithNewTargetCallback;
AtomicStringRef* m_name;
NativeFunctionPointer m_nativeFunction;
NativeFunctionWithNewTargetPointer m_nativeFunctionWithNewTarget;
size_t m_argumentCount;

NativeFunctionInfo(AtomicStringRef* name, NativeFunctionPointer fn, size_t argc, bool isStrict = true, bool isConstructor = true)
: m_isStrict(isStrict)
, m_isConstructor(isConstructor)
, m_hasWithNewTargetCallback(false)
, m_name(name)
, m_nativeFunction(fn)
, m_nativeFunctionWithNewTarget(nullptr)
, m_argumentCount(argc)
{
}

NativeFunctionInfo(AtomicStringRef* name, NativeFunctionWithNewTargetPointer fn, size_t argc, bool isStrict = true, bool isConstructor = true)
: m_isStrict(isStrict)
, m_isConstructor(isConstructor)
, m_hasWithNewTargetCallback(true)
, m_name(name)
, m_nativeFunction(nullptr)
, m_nativeFunctionWithNewTarget(fn)
, m_argumentCount(argc)
{
}
Expand Down
21 changes: 21 additions & 0 deletions test/cctest/testapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,27 @@ TEST(Object, ConstructorName)
testObj);
}

TEST(FunctionObject, Consturct)
{
FunctionObjectRef* fn = Evaluator::execute(g_context.get(), [](ExecutionStateRef* state) -> ValueRef* {
FunctionObjectRef::NativeFunctionInfo nativeFunctionInfo(AtomicStringRef::create(g_context.get(), "test"),
[](ExecutionStateRef* state, ValueRef* thisValue, size_t argc, ValueRef** argv, OptionalRef<ObjectRef> newTarget) -> ValueRef* {
EXPECT_TRUE(newTarget.hasValue());
ObjectRef* obj = ObjectRef::create(state, newTarget->asFunctionObject()->getFunctionPrototype(state)->asObject());
return obj;
},
0, true, true);
return FunctionObjectRef::create(state, nativeFunctionInfo);
})
.result->asFunctionObject();

Evaluator::execute(g_context.get(), [](ExecutionStateRef* state, FunctionObjectRef* fn) -> ValueRef* {
ObjectRef* obj = fn->construct(state, 0, nullptr)->asObject();
EXPECT_TRUE(obj->instanceOf(state, fn));
},
fn);
}

TEST(ObjectTemplate, Basic1)
{
ObjectTemplateRef* tpl = ObjectTemplateRef::create();
Expand Down