Support Builtins ShareObject/ShareFunction

Signed-off-by: lukai <lukai25@huawei.com>
Change-Id: Ie92394b6d6d355f1e0e4ff19025d18dd11d8f0a4
This commit is contained in:
lukai 2023-12-07 22:41:55 +08:00 committed by Zhenyu Pan
parent fed6d61b8b
commit 896d495a92
13 changed files with 378 additions and 4 deletions

View File

@ -586,7 +586,9 @@ ecma_source = [
"ecmascript/builtins/builtins_set.cpp",
"ecmascript/builtins/builtins_sharedarraybuffer.cpp",
"ecmascript/builtins/builtins_string.cpp",
"ecmascript/builtins/builtins_shared_function.cpp",
"ecmascript/builtins/builtins_string_iterator.cpp",
"ecmascript/builtins/builtins_shared_object.cpp",
"ecmascript/builtins/builtins_symbol.cpp",
"ecmascript/builtins/builtins_typedarray.cpp",
"ecmascript/builtins/builtins_weak_map.cpp",

View File

@ -52,7 +52,9 @@
#include "ecmascript/builtins/builtins_set.h"
#include "ecmascript/builtins/builtins_sharedarraybuffer.h"
#include "ecmascript/builtins/builtins_string.h"
#include "ecmascript/builtins/builtins_shared_function.h"
#include "ecmascript/builtins/builtins_string_iterator.h"
#include "ecmascript/builtins/builtins_shared_object.h"
#include "ecmascript/builtins/builtins_symbol.h"
#include "ecmascript/builtins/builtins_typedarray.h"
#include "ecmascript/builtins/builtins_weak_map.h"
@ -128,6 +130,8 @@ using Boolean = builtins::BuiltinsBoolean;
using BuiltinsLazyCallback = builtins::BuiltinsLazyCallback;
using BuiltinsMap = builtins::BuiltinsMap;
using BuiltinsSet = builtins::BuiltinsSet;
using BuiltinsSharedObject = builtins::BuiltinsSharedObject;
using BuiltinsSharedFunction = builtins::BuiltinsSharedFunction;
using BuiltinsWeakMap = builtins::BuiltinsWeakMap;
using BuiltinsWeakSet = builtins::BuiltinsWeakSet;
using BuiltinsWeakRef = builtins::BuiltinsWeakRef;
@ -188,6 +192,106 @@ using SharedArrayBuffer = builtins::BuiltinsSharedArrayBuffer;
using BuiltinsAsyncIterator = builtins::BuiltinsAsyncIterator;
using AsyncGeneratorObject = builtins::BuiltinsAsyncGenerator;
void Builtins::InitializeForSharing(const JSHandle<GlobalEnv> &env)
{
[[maybe_unused]] EcmaHandleScope scope(thread_);
JSHandle<JSTaggedValue> nullHandle(thread_, JSTaggedValue::Null());
// SharedObject.prototype[hclass]
JSHandle<JSHClass> sobjPrototypeHClass = factory_->NewEcmaHClass(JSSharedObject::SIZE, JSType::JS_SHARED_OBJECT,
nullHandle);
// SharedObject.prototype
JSHandle<JSObject> sobjFuncPrototype =
factory_->NewJSObjectWithInit(sobjPrototypeHClass);
JSHandle<JSTaggedValue> sobjFuncPrototypeVal(sobjFuncPrototype);
// SharedObject.prototype_or_hclass
JSHandle<JSHClass> sobjIHClass =
factory_->NewEcmaHClass(JSSharedObject::SIZE, JSType::JS_SHARED_OBJECT, sobjFuncPrototypeVal);
// SharedFunction.prototype_or_hclass
JSHandle<JSHClass> sfuncPrototypeHClass(
factory_->NewEcmaHClass(JSSharedFunction::SIZE, JSType::JS_SHARED_FUNCTION, sobjFuncPrototypeVal));
InitializeSharedFunciton(env, sfuncPrototypeHClass);
InitializeSharedObject(env, sobjIHClass, sobjFuncPrototype);
env->SetSharedObjectFunctionPrototype(thread_, sobjFuncPrototype);
}
void Builtins::InitializeSharedObject(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &sobjIHClass,
const JSHandle<JSObject> &sobjFuncPrototype) const
{
[[maybe_unused]] EcmaHandleScope scope(thread_);
// SharedObject constructor (forbidden use NewBuiltinConstructor)
JSHandle<JSFunction> sobjectFunction =
factory_->NewJSSharedFunction(env, reinterpret_cast<void *>(BuiltinsSharedObject::SharedObjectConstructor),
FunctionKind::BUILTIN_CONSTRUCTOR);
InitializeSharedCtor(env, sobjIHClass, sobjectFunction, "SharedObject", FunctionLength::ONE);
env->SetSharedObjectFunction(thread_, sobjectFunction);
// sObject method.
for (const base::BuiltinFunctionEntry &entry: Object::GetObjectFunctions()) {
SetSharedFunction(env, JSHandle<JSObject>(sobjectFunction), entry.GetName(), entry.GetEntrypoint(),
entry.GetLength(), entry.GetBuiltinStubId());
}
// sObject.prototype method
JSHandle<JSObject> sobjFuncPrototypeObj(sobjFuncPrototype);
for (const base::BuiltinFunctionEntry &entry: Object::GetObjectPrototypeFunctions()) {
SetSharedFunction(env, sobjFuncPrototypeObj, entry.GetName(), entry.GetEntrypoint(),
entry.GetLength(), entry.GetBuiltinStubId());
}
// B.2.2.1 sObject.prototype.__proto__
JSHandle<JSTaggedValue> protoKey(factory_->NewFromASCII("__proto__"));
JSHandle<JSTaggedValue> protoGetter =
CreateSharedGetter(env, Object::ProtoGetter, "__proto__", FunctionLength::ZERO);
JSHandle<JSTaggedValue> protoSetter =
CreateSharedSetter(env, Object::ProtoSetter, "__proto__", FunctionLength::ONE);
SetSharedAccessor(sobjFuncPrototypeObj, protoKey, protoGetter, protoSetter);
}
void Builtins::InitializeSharedFunciton(const JSHandle<GlobalEnv> &env,
const JSHandle<JSHClass> &sfuncPrototypeHClass) const
{
[[maybe_unused]] EcmaHandleScope scope(thread_);
// Initialize SharedFunction.prototype
JSHandle<JSFunction> sfuncPrototype = factory_->NewJSFunctionByHClass(
reinterpret_cast<void *>(Function::FunctionPrototypeInvokeSelf), sfuncPrototypeHClass);
// SharedFunction.prototype.name = ""
SetSharedFunctionName(sfuncPrototype, thread_->GlobalConstants()->GetHandledEmptyString());
// SharedFunction.prototype.length = 0
SetSharedFunctionLength(sfuncPrototype, FunctionLength::ZERO);
// SharedFunction.prototype_or_hclass
JSHandle<JSHClass> sfuncIHClass = factory_->NewEcmaHClass(JSSharedFunction::SIZE, JSType::JS_SHARED_FUNCTION,
JSHandle<JSTaggedValue>(sfuncPrototype));
sfuncIHClass->SetConstructor(true);
// new SharedFunction() (forbidden use NewBuiltinConstructor)
JSHandle<JSFunction> sfuncFunction =
factory_->NewJSFunctionByHClass(reinterpret_cast<void *>(BuiltinsSharedFunction::SharedFunctionConstructor),
sfuncIHClass, FunctionKind::BUILTIN_CONSTRUCTOR);
InitializeSharedCtor(env, sfuncIHClass, sfuncFunction, "SharedFunction", FunctionLength::ONE);
env->SetSharedFunctionPrototype(thread_, sfuncPrototype);
JSHandle<JSHClass> sharedConstructorClass =
factory_->NewEcmaHClass(JSSharedFunction::SIZE, JSType::JS_SHARED_FUNCTION, env->GetSharedFunctionPrototype());
sharedConstructorClass->SetConstructor(true);
env->SetSharedConstructorClass(thread_, sharedConstructorClass);
JSHandle<JSObject> sfuncPrototypeObj(sfuncPrototype);
StrictModeForbiddenAccessCallerArguments(env, sfuncPrototypeObj);
// Function.prototype method
// 19.2.3.1 Function.prototype.apply ( thisArg, argArray )
SetSharedFunction(env, sfuncPrototypeObj, "apply", Function::FunctionPrototypeApply, FunctionLength::TWO,
BUILTINS_STUB_ID(FunctionPrototypeApply));
// 19.2.3.2 Function.prototype.bind ( thisArg , ...args)
SetSharedFunction(env, sfuncPrototypeObj, "bind", Function::FunctionPrototypeBind, FunctionLength::ONE);
// 19.2.3.3 Function.prototype.call (thisArg , ...args)
SetSharedFunction(env, sfuncPrototypeObj, "call", Function::FunctionPrototypeCall, FunctionLength::ONE);
// 19.2.3.5 Function.prototype.toString ( )
SetSharedFunction(env, sfuncPrototypeObj, thread_->GlobalConstants()->GetHandledToStringString(),
Function::FunctionPrototypeToString, FunctionLength::ZERO);
}
void Builtins::Initialize(const JSHandle<GlobalEnv> &env, JSThread *thread, bool lazyInit, bool isRealm)
{
thread_ = thread;
@ -311,7 +415,7 @@ void Builtins::Initialize(const JSHandle<GlobalEnv> &env, JSThread *thread, bool
InitializeBoolean(env, primRefObjHClass);
InitializeRegExp(env);
InitializeString(env, objFuncPrototypeVal);
InitializeForSharing(env);
JSHandle<JSHClass> argumentsClass = factory_->CreateJSArguments(env);
env->SetArgumentsClass(thread_, argumentsClass);
SetArgumentsSharedAccessor(env);
@ -2506,10 +2610,9 @@ JSHandle<JSFunction> Builtins::NewFunction(const JSHandle<GlobalEnv> &env, const
FunctionKind::NORMAL_FUNCTION, builtinId, MemSpaceType::NON_MOVABLE);
JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
JSHandle<JSFunctionBase> baseFunction(function);
JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined());
JSFunction::SetFunctionName(thread_, baseFunction, key, handleUndefine);
auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
JSFunction::SetFunctionName(thread_, baseFunction, key, globalConst->GetHandledUndefined());
if (IS_TYPED_BUILTINS_ID(builtinId)) {
auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
globalConst->SetConstant(GET_TYPED_CONSTANT_INDEX(builtinId), function);
}
return function;
@ -3541,4 +3644,100 @@ void Builtins::InitializeDefaultExportOfScript(const JSHandle<GlobalEnv> &env) c
env->SetExportOfScript(thread_, obj);
return;
}
void Builtins::SetSharedFunctionName(const JSHandle<JSFunction> &ctor, std::string_view name) const
{
JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8(name));
SetSharedFunctionName(ctor, nameString);
}
void Builtins::SetSharedFunctionName(const JSHandle<JSFunction> &ctor, const JSHandle<JSTaggedValue> &name) const
{
const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
JSHandle<JSTaggedValue> nameKey = globalConst->GetHandledNameString();
PropertyDescriptor nameDesc(thread_, name, false, false, false);
JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(ctor), nameKey, nameDesc);
}
void Builtins::SetSharedFunctionLength(const JSHandle<JSFunction> &ctor, int length) const
{
const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
JSHandle<JSTaggedValue> lengthKeyHandle = globalConst->GetHandledLengthString();
JSTaggedValue taggedLength(length);
PropertyDescriptor lengthDesc(thread_, JSHandle<JSTaggedValue>(thread_, taggedLength), false, false, false);
JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(ctor), lengthKeyHandle, lengthDesc);
}
void Builtins::InitializeSharedCtor(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &protoHClass,
const JSHandle<JSFunction> &ctor, std::string_view name, int length) const
{
const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
SetSharedFunctionLength(ctor, length);
SetSharedFunctionName(ctor, name);
JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(ctor), false, false, false);
JSHandle<JSObject> prototype(thread_, protoHClass->GetProto());
JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor);
ctor->SetFunctionPrototype(thread_, protoHClass.GetTaggedValue());
JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
PropertyDescriptor globalDesc(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8(name));
JSObject::DefineOwnProperty(thread_, globalObject, nameString, globalDesc);
}
JSHandle<JSFunction> Builtins::NewSharedFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &key,
EcmaEntrypoint func, int length,
kungfu::BuiltinsStubCSigns::ID builtinId) const
{
JSHandle<JSFunction> function = factory_->NewJSSharedFunction(env, reinterpret_cast<void *>(func),
FunctionKind::NORMAL_FUNCTION, builtinId, MemSpaceType::NON_MOVABLE);
SetSharedFunctionLength(function, length);
SetSharedFunctionName(function, key);
return function;
}
void Builtins::SetSharedFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, std::string_view key,
EcmaEntrypoint func, int length, kungfu::BuiltinsStubCSigns::ID builtinId) const
{
JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
SetSharedFunction(env, obj, keyString, func, length, builtinId);
}
void Builtins::SetSharedFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj,
const JSHandle<JSTaggedValue> &key, EcmaEntrypoint func, int length,
kungfu::BuiltinsStubCSigns::ID builtinId) const
{
JSHandle<JSFunction> function(NewSharedFunction(env, key, func, length, builtinId));
PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(function), false, false, false);
JSObject::DefineOwnProperty(thread_, obj, key, descriptor);
}
void Builtins::SetSharedAccessor(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key,
const JSHandle<JSTaggedValue> &getter, const JSHandle<JSTaggedValue> &setter) const
{
JSHandle<AccessorData> accessor = factory_->NewAccessorData();
accessor->SetGetter(thread_, getter);
accessor->SetSetter(thread_, setter);
PropertyAttributes attr = PropertyAttributes::DefaultAccessor(false, false, false);
JSObject::AddAccessor(thread_, JSHandle<JSTaggedValue>::Cast(obj), key, accessor, attr);
}
JSHandle<JSTaggedValue> Builtins::CreateSharedGetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func,
std::string_view name, int length) const
{
JSHandle<JSTaggedValue> funcName(factory_->NewFromUtf8(name));
auto getter = NewSharedFunction(env, funcName, func, length);
return JSHandle<JSTaggedValue>(getter);
}
JSHandle<JSTaggedValue> Builtins::CreateSharedSetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func,
std::string_view name, int length) const
{
JSHandle<JSTaggedValue> funcName(factory_->NewFromUtf8(name));
auto setter = NewSharedFunction(env, funcName, func, length);
return JSHandle<JSTaggedValue>(setter);
}
} // namespace panda::ecmascript

View File

@ -336,6 +336,38 @@ private:
EcmaEntrypoint func, int length) const;
void SetNonConstantObject(const JSHandle<JSObject> &obj, std::string_view key,
JSHandle<JSTaggedValue> &value) const;
void InitializeForSharing(const JSHandle<GlobalEnv> &env);
void InitializeSharedObject(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &sobjIHClass,
const JSHandle<JSObject> &sobjFuncPrototype) const;
void InitializeSharedFunciton(const JSHandle<GlobalEnv> &env,
const JSHandle<JSHClass> &sfuncPrototypeHClass) const;
void InitializeSharedCtor(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &protoHClass,
const JSHandle<JSFunction> &ctor, std::string_view name, int length) const;
void SetSharedFunctionName(const JSHandle<JSFunction> &ctor, std::string_view name) const;
void SetSharedFunctionName(const JSHandle<JSFunction> &ctor, const JSHandle<JSTaggedValue> &name) const;
void SetSharedFunctionLength(const JSHandle<JSFunction> &ctor, int length) const;
JSHandle<JSFunction> NewSharedFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &key,
EcmaEntrypoint func, int length,
kungfu::BuiltinsStubCSigns::ID builtinId =
kungfu::BuiltinsStubCSigns::INVALID) const;
void SetSharedFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, std::string_view key,
EcmaEntrypoint func, int length, kungfu::BuiltinsStubCSigns::ID builtinId =
kungfu::BuiltinsStubCSigns::INVALID) const;
void SetSharedFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj,
const JSHandle<JSTaggedValue> &key, EcmaEntrypoint func, int length,
kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID) const;
void SetSharedAccessor(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key,
const JSHandle<JSTaggedValue> &getter, const JSHandle<JSTaggedValue> &setter) const;
JSHandle<JSTaggedValue> CreateSharedGetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func,
std::string_view name, int length) const;
JSHandle<JSTaggedValue> CreateSharedSetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func,
std::string_view name, int length) const;
friend class builtins::BuiltinsLazyCallback;
};

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/builtins/builtins_shared_function.h"
#include "ecmascript/ecma_vm.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsSharedFunction::SharedFunctionConstructor(EcmaRuntimeCallInfo *argv)
{
// not support
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Function, Constructor);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
THROW_TYPE_ERROR_AND_RETURN(thread, "Not support eval. Forbidden using new SharedFuction()/SharedFuction().",
JSTaggedValue::Exception());
}
} // namespace panda::ecmascript::builtins

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMASCRIPT_BUILTINS_BUILTINS_SHARED_FUNCTION_H
#define ECMASCRIPT_BUILTINS_BUILTINS_SHARED_FUNCTION_H
#include "ecmascript/base/builtins_base.h"
#include "ecmascript/ecma_runtime_call_info.h"
namespace panda::ecmascript::builtins {
class BuiltinsSharedFunction : public base::BuiltinsBase {
public:
static JSTaggedValue SharedFunctionConstructor(EcmaRuntimeCallInfo *argv);
};
} // namespace panda::ecmascript::builtins
#endif // ECMASCRIPT_BUILTINS_BUILTINS_SHARED_FUNCTION_H

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/builtins/builtins_shared_object.h"
#include "ecmascript/ecma_vm.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsSharedObject::SharedObjectConstructor(EcmaRuntimeCallInfo *argv)
{
// not support
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Function, Constructor);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
THROW_TYPE_ERROR_AND_RETURN(thread, "Not support eval. Forbidden using new SharedObject()/SharedObject().",
JSTaggedValue::Exception());
}
} // namespace panda::ecmascript::builtins

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMASCRIPT_BUILTINS_BUILTINS_SHARED_OBJECT_H
#define ECMASCRIPT_BUILTINS_BUILTINS_SHARED_OBJECT_H
#include "ecmascript/base/builtins_base.h"
#include "ecmascript/ecma_runtime_call_info.h"
namespace panda::ecmascript::builtins {
class BuiltinsSharedObject : public base::BuiltinsBase {
public:
static JSTaggedValue SharedObjectConstructor(EcmaRuntimeCallInfo *argv);
};
} // namespace panda::ecmascript::builtins
#endif // ECMASCRIPT_BUILTINS_BUILTINS_SHARED_OBJECT_H

View File

@ -27,8 +27,11 @@
V(JSTaggedValue, ObjectFunctionPrototype, OBJECT_FUNCTION_PROTOTYPE_INDEX) \
V(JSTaggedValue, ObjectFunctionPrototypeClass, OBJECT_FUNCTION_PROTOTYPE_CLASS_INDEX) \
V(JSTaggedValue, SharedObjectFunction, SHARED_OBJECT_FUNCTION_INDEX) \
V(JSTaggedValue, SharedObjectFunctionPrototype, SHARED_OBJECT_FUNCTION_PROTOTYPE_INDEX) \
V(JSTaggedValue, FunctionFunction, FUNCTION_FUNCTION_INDEX) \
V(JSTaggedValue, FunctionPrototype, FUNCTION_PROTOTYPE_INDEX) \
V(JSTaggedValue, SharedFunctionPrototype, SHARED_FUNCTION_PROTOTYPE_INDEX) \
V(JSTaggedValue, SharedConstructorClass, SHARED_CONSTRUCTOR_CLASS_INDEX) \
V(JSTaggedValue, NumberFunction, NUMBER_FUNCTION_INDEX) \
V(JSTaggedValue, NumberPrototype, NUMBER_PROTOTYPE_INDEX) \
V(JSTaggedValue, BigIntFunction, BIGINT_FUNCTION_INDEX) \

View File

@ -465,6 +465,7 @@ public:
static constexpr size_t OWNER_ID_OFFSET = JSFunction::SIZE;
ACCESSORS_PRIMITIVE_FIELD(OwnerID, uint64_t, OWNER_ID_OFFSET, LAST_OFFSET)
DEFINE_ALIGN_SIZE(LAST_OFFSET);
DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSFunction, OWNER_ID_OFFSET, OWNER_ID_OFFSET)
DECL_CHECK_OWNERSHIP();
DECL_SET_OWNERSHIP();
DECL_DUMP()

View File

@ -27,6 +27,7 @@ public:
static constexpr size_t OWNER_ID_OFFSET = JSObject::SIZE;
ACCESSORS_PRIMITIVE_FIELD(OwnerID, uint64_t, OWNER_ID_OFFSET, LAST_OFFSET)
DEFINE_ALIGN_SIZE(LAST_OFFSET);
DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, OWNER_ID_OFFSET, OWNER_ID_OFFSET)
DECL_CHECK_OWNERSHIP();
DECL_SET_OWNERSHIP();
DECL_DUMP()

View File

@ -142,6 +142,11 @@ public:
case JSType::JS_ITERATOR:
JSObject::Cast(object)->VisitRangeSlot<visitType>(visitor);
break;
case JSType::JS_SHARED_OBJECT: {
auto jsSharedObject = JSSharedObject::Cast(object);
jsSharedObject->VisitRangeSlot<visitType>(visitor);
break;
}
case JSType::JS_ASYNC_FROM_SYNC_ITERATOR:
JSAsyncFromSyncIterator::Cast(object)->VisitRangeSlot<visitType>(visitor);
break;

View File

@ -1482,6 +1482,17 @@ JSHandle<JSObject> ObjectFactory::OrdinaryNewJSObjectCreate(const JSHandle<JSTag
return newObj;
}
JSHandle<JSFunction> ObjectFactory::NewJSSharedFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc,
FunctionKind kind, kungfu::BuiltinsStubCSigns::ID builtinId, MemSpaceType spaceType)
{
JSHandle<Method> method = NewMethodForNativeFunction(nativeFunc, kind, builtinId, spaceType);
JSHandle<JSHClass> hclass = JSHandle<JSHClass>::Cast(env->GetSharedConstructorClass());
JSHandle<JSFunction> sfunc = NewJSFunctionByHClass(method, hclass);
uint32_t threadID = thread_->GetThreadId();
JSHandle<JSSharedFunction>(sfunc)->SetOwnerID(0xFFFFFFFF00000000 | threadID);
return sfunc;
}
JSHandle<JSFunction> ObjectFactory::NewJSFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc,
FunctionKind kind, kungfu::BuiltinsStubCSigns::ID builtinId,
MemSpaceType spaceType)

View File

@ -227,6 +227,10 @@ public:
FunctionKind kind = FunctionKind::NORMAL_FUNCTION,
kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID,
MemSpaceType spaceType = OLD_SPACE);
JSHandle<JSFunction> NewJSSharedFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr,
FunctionKind kind = FunctionKind::NORMAL_FUNCTION,
kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID,
MemSpaceType spaceType = OLD_SPACE);
// use for method
JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<Method> &method);