Commit cd0ca02a authored by Manos Koukoutos's avatar Manos Koukoutos Committed by V8 LUCI CQ

[wasm] Add WasmApiFunctionRef object

This object will be used for the 'ref' field of WasmCapiFunctionData and
WasmJSFunctionData, replacing the currently used pair.
Design doc: https://bit.ly/3jEVgzz

Bug: v8:11510
Change-Id: Ic5dec88458b562883d571b3463269b2308f489c5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3236718Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77575}
parent aa5c6889
......@@ -1929,6 +1929,14 @@ void WasmJSFunctionData::WasmJSFunctionDataPrint(std::ostream& os) {
os << "\n";
}
void WasmApiFunctionRef::WasmApiFunctionRefPrint(std::ostream& os) {
PrintHeader(os, "WasmApiFunctionRef");
os << "\n - isolate_root: " << reinterpret_cast<void*>(foreign_address());
os << "\n - native_context: " << Brief(native_context());
os << "\n - callable: " << Brief(callable());
os << "\n";
}
void WasmCapiFunctionData::WasmCapiFunctionDataPrint(std::ostream& os) {
PrintHeader(os, "WasmCapiFunctionData");
WasmFunctionDataPrint(os);
......
......@@ -1473,6 +1473,22 @@ Handle<WasmTypeInfo> Factory::NewWasmTypeInfo(
return handle(result, isolate());
}
Handle<WasmApiFunctionRef> Factory::NewWasmApiFunctionRef(
Handle<JSReceiver> callable) {
Map map = *wasm_api_function_ref_map();
auto result = WasmApiFunctionRef::cast(AllocateRawWithImmortalMap(
map.instance_size(), AllocationType::kOld, map));
DisallowGarbageCollection no_gc;
result.set_foreign_address(isolate(), isolate()->isolate_root());
result.set_native_context(*isolate()->native_context());
if (!callable.is_null()) {
result.set_callable(*callable);
} else {
result.set_callable(*undefined_value());
}
return handle(result, isolate());
}
Handle<WasmJSFunctionData> Factory::NewWasmJSFunctionData(
Address opt_call_target, Handle<JSReceiver> callable, int return_count,
int parameter_count, Handle<PodArray<wasm::ValueType>> serialized_sig,
......
......@@ -576,6 +576,7 @@ class V8_EXPORT_PRIVATE Factory : public FactoryBase<Factory> {
Handle<Code> export_wrapper, Handle<WasmInstanceObject> instance,
Address call_target, Handle<Object> ref, int func_index,
Address sig_address, int wrapper_budget);
Handle<WasmApiFunctionRef> NewWasmApiFunctionRef(Handle<JSReceiver> callable);
// {opt_call_target} is kNullAddress for JavaScript functions, and
// non-null for exported Wasm functions.
Handle<WasmJSFunctionData> NewWasmJSFunctionData(
......
......@@ -57,6 +57,7 @@ namespace internal {
IF_WASM(V, WasmIndirectFunctionTable) \
IF_WASM(V, WasmInstanceObject) \
IF_WASM(V, WasmJSFunctionData) \
IF_WASM(V, WasmApiFunctionRef) \
IF_WASM(V, WasmStruct) \
IF_WASM(V, WasmTypeInfo)
......
......@@ -506,6 +506,8 @@ bool Heap::CreateInitialMaps() {
WasmCapiFunctionData::kSize, wasm_capi_function_data)
IF_WASM(ALLOCATE_MAP, WASM_EXPORTED_FUNCTION_DATA_TYPE,
WasmExportedFunctionData::kSize, wasm_exported_function_data)
IF_WASM(ALLOCATE_MAP, WASM_API_FUNCTION_REF_TYPE, WasmApiFunctionRef::kSize,
wasm_api_function_ref)
IF_WASM(ALLOCATE_MAP, WASM_JS_FUNCTION_DATA_TYPE, WasmJSFunctionData::kSize,
wasm_js_function_data)
IF_WASM(ALLOCATE_MAP, WASM_TYPE_INFO_TYPE, WasmTypeInfo::kSize,
......
......@@ -368,6 +368,8 @@ VisitorId Map::GetVisitorId(Map map) {
return kVisitWasmTypeInfo;
case WASM_JS_FUNCTION_DATA_TYPE:
return kVisitWasmJSFunctionData;
case WASM_API_FUNCTION_REF_TYPE:
return kVisitWasmApiFunctionRef;
case WASM_EXPORTED_FUNCTION_DATA_TYPE:
return kVisitWasmExportedFunctionData;
case WASM_CAPI_FUNCTION_DATA_TYPE:
......
......@@ -75,6 +75,7 @@ enum InstanceType : uint16_t;
IF_WASM(V, WasmIndirectFunctionTable) \
IF_WASM(V, WasmInstanceObject) \
IF_WASM(V, WasmJSFunctionData) \
IF_WASM(V, WasmApiFunctionRef) \
IF_WASM(V, WasmStruct) \
IF_WASM(V, WasmTypeInfo) \
V(WeakCell)
......
......@@ -234,6 +234,7 @@ class ZoneForwardList;
IF_WASM(V, WasmGlobalObject) \
IF_WASM(V, WasmInstanceObject) \
IF_WASM(V, WasmJSFunctionData) \
IF_WASM(V, WasmApiFunctionRef) \
IF_WASM(V, WasmMemoryObject) \
IF_WASM(V, WasmModuleObject) \
IF_WASM(V, WasmObject) \
......
......@@ -641,6 +641,24 @@ class WasmJSFunctionData::BodyDescriptor final : public BodyDescriptorBase {
static inline int SizeOf(Map map, HeapObject object) { return kSize; }
};
class WasmApiFunctionRef::BodyDescriptor final : public BodyDescriptorBase {
public:
static bool IsValidSlot(Map map, HeapObject obj, int offset) {
UNREACHABLE();
}
template <typename ObjectVisitor>
static inline void IterateBody(Map map, HeapObject obj, int object_size,
ObjectVisitor* v) {
Foreign::BodyDescriptor::IterateBody<ObjectVisitor>(map, obj, object_size,
v);
IteratePointers(obj, WasmFunctionData::kStartOfStrongFieldsOffset,
kEndOfStrongFieldsOffset, v);
}
static inline int SizeOf(Map map, HeapObject object) { return kSize; }
};
class WasmExportedFunctionData::BodyDescriptor final
: public BodyDescriptorBase {
public:
......@@ -1060,6 +1078,9 @@ ReturnType BodyDescriptorApply(InstanceType type, T1 p1, T2 p2, T3 p3, T4 p4) {
case WASM_JS_FUNCTION_DATA_TYPE:
return Op::template apply<WasmJSFunctionData::BodyDescriptor>(p1, p2, p3,
p4);
case WASM_API_FUNCTION_REF_TYPE:
return Op::template apply<WasmApiFunctionRef::BodyDescriptor>(p1, p2, p3,
p4);
case WASM_STRUCT_TYPE:
return Op::template apply<WasmStruct::BodyDescriptor>(p1, p2, p3, p4);
case WASM_TYPE_INFO_TYPE:
......
......@@ -115,6 +115,7 @@ class Symbol;
IF_WASM(V, Map, wasm_exported_function_data_map, \
WasmExportedFunctionDataMap) \
IF_WASM(V, Map, wasm_js_function_data_map, WasmJSFunctionDataMap) \
IF_WASM(V, Map, wasm_api_function_ref_map, WasmApiFunctionRefMap) \
IF_WASM(V, Map, wasm_type_info_map, WasmTypeInfoMap) \
V(Map, weak_fixed_array_map, WeakFixedArrayMap) \
V(Map, weak_array_list_map, WeakArrayListMap) \
......
......@@ -47,6 +47,7 @@ TQ_OBJECT_CONSTRUCTORS_IMPL(WasmModuleObject)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmTableObject)
TQ_OBJECT_CONSTRUCTORS_IMPL(AsmWasmData)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmFunctionData)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmApiFunctionRef)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmTypeInfo)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmStruct)
TQ_OBJECT_CONSTRUCTORS_IMPL(WasmArray)
......
......@@ -733,6 +733,17 @@ class WasmExportedFunctionData
TQ_OBJECT_CONSTRUCTORS(WasmExportedFunctionData)
};
class WasmApiFunctionRef
: public TorqueGeneratedWasmApiFunctionRef<WasmApiFunctionRef, Foreign> {
public:
// Dispatched behavior.
DECL_PRINTER(WasmApiFunctionRef)
class BodyDescriptor;
TQ_OBJECT_CONSTRUCTORS(WasmApiFunctionRef)
};
// Information for a WasmJSFunction which is referenced as the function data of
// the SharedFunctionInfo underlying the function. For details please see the
// {SharedFunctionInfo::HasWasmJSFunctionData} predicate.
......
......@@ -11,6 +11,16 @@ type ManagedWasmNativeModule extends Foreign
extern class WasmInstanceObject extends JSObject;
// Represents the context of a function that is defined through the JS or C
// APIs. Corresponds to the WasmInstanceObject passed to a Wasm function
// reference.
// The {foreign_address} field inherited from {Foreign} points the IsolateRoots
// of the defining isolate.
extern class WasmApiFunctionRef extends Foreign {
native_context: NativeContext;
callable: JSReceiver|Undefined;
}
extern class WasmFunctionData extends Foreign {
// This is the "reference" value that must be passed along in the "instance"
// register when calling the given function. It is either the target instance,
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment