Skip to content
Open
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
9 changes: 3 additions & 6 deletions deps/v8/src/api/api-wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ Context::~Context() {
Val::Val(ValKind kind, value value) : kind_(kind), value_(value) {}

Val::Val() : kind_(ANYREF) { value_.ref = nullptr; }
Val::Val(Val&& val) : Val(val.kind_, val.value_) {
}
Val::Val(Val&& val) : Val(val.kind_, val.value_) {}

Val::Val(int32_t i) : kind_(I32) { value_.i32 = i; }
Val::Val(int64_t i) : kind_(I64) { value_.i64 = i; }
Expand Down Expand Up @@ -170,7 +169,7 @@ i::Address FuncData::v8_callback(
) {
FuncData* self = reinterpret_cast<FuncData*>(data);
i::Isolate* isolate = self->isolate;
HandleScope scope(reinterpret_cast<v8::Isolate*>(isolate));
i::HandleScope scope(isolate);

int num_param_types = self->param_count;
int num_result_types = self->result_count;
Expand Down Expand Up @@ -203,9 +202,7 @@ i::Address FuncData::v8_callback(
if (raw == i::kNullAddress) {
params[i] = Val(nullptr);
} else {
i::JSReceiver raw_obj = i::JSReceiver::cast(i::Object(raw));
i::Handle<i::JSReceiver> obj(raw_obj, raw_obj.GetIsolate());
params[i] = Val(reinterpret_cast<void*>(obj->address()));
params[i] = Val(reinterpret_cast<void*>(raw));
}
break;
}
Expand Down
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@
'src/udp_wrap.cc',
'src/util.cc',
'src/uv.cc',
# wasm napi sources
'wasm_node_api/wasm_node_api.cc',
# headers to make for a more pleasant IDE experience
'src/aliased_buffer.h',
'src/async_wrap.h',
Expand Down
12 changes: 12 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
#include "node_v8_platform-inl.h"
#include "node_version.h"

// TODO(ohadrau): Move this to a configure flag
#define WASM_BUILTIN_NODE_API 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a TODO comment to move this to a configure flag.


#if WASM_BUILTIN_NODE_API
#include "../wasm_node_api/wasm_node_api.h"
#endif

#if HAVE_OPENSSL
#include "node_crypto.h"
#endif
Expand Down Expand Up @@ -421,6 +428,11 @@ MaybeLocal<Value> StartMainThreadExecution(Environment* env) {
// To allow people to extend Node in different ways, this hook allows
// one to drop a file lib/_third_party_main.js into the build
// directory which will be executed instead of Node's normal loading.
#if WASM_BUILTIN_NODE_API
v8::Isolate* isolate = env->isolate();
wasm_napi::RegisterNapiBuiltins(isolate);
#endif

if (NativeModuleEnv::Exists("_third_party_main")) {
return StartExecution(env, "internal/main/run_third_party_main");
}
Expand Down
27 changes: 27 additions & 0 deletions test_wasm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SYSROOT=$$HOME/Projects/wasi-sysroot/sysroot
TARGET=wasm32-wasi

COMMA=,

SOURCES=test_napi.c ../wasm_node_api/wasm_napi_lib/node_api_overrides.c

OBJECTS=$(SOURCES:.c=.o)

IMPORTS=$(shell cat import.syms)
EXPORTS=$(shell cat export.syms)

CFLAGS=-I../wasm_node_api/wasm_napi_lib --sysroot=$(SYSROOT) --target=$(TARGET)
IMPORTFLAGS=-allow-undefined-file import.syms
EXPORTFLAGS=$(addprefix --export=, $(EXPORTS)) --export-table
LDFLAGS=--entry=main $(IMPORTFLAGS) $(EXPORTFLAGS) -L$(SYSROOT)/lib/wasm32-wasi -lc

all: test_wasm_napi.node.wasm

clean:
rm -f $(OBJECTS) test-napi.wasm

test_wasm_napi.wasm: $(OBJECTS)
wasm-ld $(LDFLAGS) -o $@ $^

%.o: %.c
clang $(CFLAGS) -o $@ -c $<
2 changes: 2 additions & 0 deletions test_wasm/export.syms
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main
Init
30 changes: 30 additions & 0 deletions test_wasm/import.syms
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
napi_create_string_utf8
create_napi_module
set_napi_module_nm_version
set_napi_module_nm_flags
set_napi_module_nm_filename
set_napi_module_nm_register_func
set_napi_module_nm_modname
set_napi_module_nm_priv
_napi_module_register
napi_create_external_arraybuffer
napi_create_external_buffer
create_property_descriptors
set_napi_property_descriptor_utf8name
set_napi_property_descriptor_name
set_napi_property_descriptor_method
set_napi_property_descriptor_getter
set_napi_property_descriptor_setter
set_napi_property_descriptor_value
set_napi_property_descriptor_attributes
set_napi_property_descriptor_data
_napi_define_properties
_napi_define_class
get_string_length
copy_string_to_wasm
create_napi_extended_error_info
_napi_get_last_error_info
napi_extended_error_info_engine_error_code
napi_extended_error_info_engine_reserved
napi_extended_error_info_error_code
napi_extended_error_info_error_message
40 changes: 40 additions & 0 deletions test_wasm/test_napi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <assert.h>
#include <node_api.h>

typedef opaque_ptr napi_env;
typedef opaque_ptr napi_callback_info;
typedef opaque_ptr napi_value;

napi_value Hello(napi_env env, napi_callback_info info) {
napi_status status;
napi_value world;
status = napi_create_string_utf8(env, "world", 5, &world);
assert(status == napi_ok);
return world;
}

#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }

napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Hello);
status = napi_define_properties(env, exports, 1, &desc);
assert(status == napi_ok);
return exports;
}

napi_module mod = {
NAPI_MODULE_VERSION,
0,
__FILE__,
Init,
"test_wasm_napi",
NULL,
{0}
};

int main(int argc, char **argv) {
napi_module_register(&mod);
return 0;
}
Binary file added test_wasm/test_wasm_napi.node.wasm
Binary file not shown.
Loading