diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index de20418..36a1b1b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,12 +20,20 @@ jobs: with: packages: build-essential cmake python3 emscripten + - uses: actions/setup-go@v6 + with: + go-version: '1.25' + - name: Build C++ (cmake) working-directory: cpp run: | emcmake cmake -S . -B build -DCMAKE_BUILD_TYPE=Release cmake --build build --config Release --parallel + - name: Build Go + run: | + GOOS=js GOARCH=wasm go build -o node/test/go-integration-test.wasm ./go/cmd/integration-test/ + - name: Install pnpm uses: pnpm/action-setup@v4 with: diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..357c33e --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/jc-lab/walink + +go 1.25.0 + +require ( + github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..84eba6c --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8= +github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= +github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= +github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= diff --git a/go/cmd/integration-test/main.go b/go/cmd/integration-test/main.go new file mode 100644 index 0000000..0e06319 --- /dev/null +++ b/go/cmd/integration-test/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "github.com/jc-lab/walink/go/wlvalue" +) + +//export add +//go:wasmexport add +func add(a, b wlvalue.Value) wlvalue.Value { + return wlvalue.FromInt32(wlvalue.ToInt32(a) + wlvalue.ToInt32(b)) +} + +//export echo_string +//go:wasmexport echo_string +func echoString(v wlvalue.Value) wlvalue.Value { + s := wlvalue.ToString(v, true) + return wlvalue.MakeString(s, true) +} + +//export call_me_back +//go:wasmexport call_me_back +func callMeBack(cb wlvalue.Value, val wlvalue.Value) wlvalue.Value { + f := cb.ToFunction() + return f(val) +} + +func main() {} diff --git a/go/wlvalue/callback.go b/go/wlvalue/callback.go new file mode 100644 index 0000000..1ef9d38 --- /dev/null +++ b/go/wlvalue/callback.go @@ -0,0 +1,21 @@ +package wlvalue + +import ( + "fmt" + "unsafe" +) + +type CallbackFunc func(args ...Value) Value + +func (v Value) ToFunction() CallbackFunc { + if !v.IsFunction() { + panic(fmt.Errorf("value(0x%x) is not a function", v)) + } + return func(args ...Value) Value { + var argsPtr uint32 + if len(args) > 0 { + argsPtr = uint32(uintptr(unsafe.Pointer(&args[0]))) + } + return walinkCallback(v, argsPtr, uint32(len(args))) + } +} diff --git a/go/wlvalue/callback_stub.go b/go/wlvalue/callback_stub.go new file mode 100644 index 0000000..0dff304 --- /dev/null +++ b/go/wlvalue/callback_stub.go @@ -0,0 +1,7 @@ +//go:build !wasm + +package wlvalue + +func walinkCallback(v Value, argsPtr uint32, argsLen uint32) Value { + panic("walinkCallback is only available in wasm") +} diff --git a/go/wlvalue/callback_wasm.go b/go/wlvalue/callback_wasm.go new file mode 100644 index 0000000..ba860da --- /dev/null +++ b/go/wlvalue/callback_wasm.go @@ -0,0 +1,7 @@ +//go:build wasm + +package wlvalue + +//export walink_callback +//go:wasmimport env walink_callback +func walinkCallback(v Value, argsPtr uint32, argsLen uint32) Value diff --git a/go/wlvalue/msgpack.go b/go/wlvalue/msgpack.go new file mode 100644 index 0000000..5f41480 --- /dev/null +++ b/go/wlvalue/msgpack.go @@ -0,0 +1,30 @@ +package wlvalue + +import ( + "bytes" + + "github.com/vmihailenco/msgpack/v5" +) + +type MsgpackOption func(d *msgpack.Decoder) error + +func MsgpackTag(tag string) MsgpackOption { + return func(d *msgpack.Decoder) error { + d.SetCustomStructTag(tag) + return nil + } +} + +func DecodeMsgpack(v Value, allowFree bool, out interface{}, options ...MsgpackOption) error { + raw := ToMsgpackBytes(v, allowFree) + d := msgpack.NewDecoder(bytes.NewReader(raw)) + for _, option := range options { + if err := option(d); err != nil { + return err + } + } + if out == nil { + return nil + } + return d.Decode(out) +} diff --git a/go/wlvalue/walink.go b/go/wlvalue/walink.go new file mode 100644 index 0000000..86960bd --- /dev/null +++ b/go/wlvalue/walink.go @@ -0,0 +1,387 @@ +package wlvalue + +import ( + "errors" + "math" + "sync" + "unsafe" +) + +type Value uint64 + +type Tag uint32 + +const ( + TagNull Tag = 0x0 + TagBoolean Tag = 0x10 + TagSint8 Tag = 0x11 + TagUint8 Tag = 0x21 + TagSint16 Tag = 0x12 + TagUint16 Tag = 0x22 + TagSint32 Tag = 0x14 + TagUint32 Tag = 0x24 + TagFloat32 Tag = 0x30 + + TagFloat64 Tag = 0x31 + TagBytes Tag = 0x01 + TagString Tag = 0x02 + TagMsgpack Tag = 0x0100 + TagError Tag = 0xffffff0 + TagFunction Tag = 0x1000000 +) + +const ( + MetaUserDefined uint32 = 0x80000000 + MetaIsAddress uint32 = 0x40000000 + MetaFreeFlag uint32 = 0x20000000 + MetaTagMask uint32 = 0x0FFFFFFF +) + +var nullPointer = unsafe.Pointer(uintptr(0)) + +type baseContainer struct { + cap uint32 + size uint32 + data [0]byte +} + +type float64Container struct { + v float64 +} + +func GetMeta(v Value) uint32 { + return uint32(v >> 32) +} + +func GetPayload32(v Value) uint32 { + return uint32(v & 0xffffffff) +} + +func GetPointer(v Value) unsafe.Pointer { + return unsafe.Pointer(uintptr(v & 0xffffffff)) +} + +func MakeValue(meta uint32, payload uint32) Value { + return Value(uint64(meta)<<32 | uint64(payload)) +} + +func GetTag(v Value) Tag { + return Tag(GetMeta(v) & MetaTagMask) +} + +func IsAddress(v Value) bool { + return (GetMeta(v) & MetaIsAddress) != 0 +} + +func (v Value) IsFunction() bool { + return GetTag(v) == TagFunction +} + +func BuildMeta(tag Tag, isAddress bool, freeFlag bool, userDefined bool) uint32 { + meta := uint32(tag) & MetaTagMask + if userDefined { + meta |= MetaUserDefined + } + if isAddress { + meta |= MetaIsAddress + } + if freeFlag { + meta |= MetaFreeFlag + } + return meta +} + +func FromAddress(ptr unsafe.Pointer, tag Tag, freeFlag bool) Value { + payload := uint32(uintptr(ptr)) + meta := BuildMeta(tag, true, freeFlag, false) + return MakeValue(meta, payload) +} + +// ---- Registry for GC retention ---- + +var ( + registryMu sync.Mutex + registry = make(map[unsafe.Pointer]interface{}) +) + +func retain(ptr unsafe.Pointer, obj interface{}) { + registryMu.Lock() + defer registryMu.Unlock() + registry[ptr] = obj +} + +func release(ptr unsafe.Pointer, free bool) interface{} { + registryMu.Lock() + defer registryMu.Unlock() + obj, ok := registry[ptr] + if ok && free { + delete(registry, ptr) + } + return obj +} + +func getPointer(v Value, allowFree bool) unsafe.Pointer { + if !IsAddress(v) { + panic(errors.New("no address")) + } + ptr := GetPointer(v) + if ptr == nullPointer { + return nullPointer + } + needFree := (GetMeta(v) & MetaFreeFlag) != 0 + if allowFree && needFree { + release(ptr, true) + } + return ptr +} + +// ---- Factories ---- + +func FromBool(b bool) Value { + var payload uint32 + if b { + payload = 1 + } + meta := BuildMeta(TagBoolean, false, false, false) + return MakeValue(meta, payload) +} + +func FromInt8(v int8) Value { + meta := BuildMeta(TagSint8, false, false, false) + return MakeValue(meta, uint32(int32(v))) +} + +func ToInt8(v Value) int8 { + return int8(int32(GetPayload32(v))) +} + +func FromUint8(v uint8) Value { + meta := BuildMeta(TagUint8, false, false, false) + return MakeValue(meta, uint32(v)) +} + +func ToUint8(v Value) uint8 { + return uint8(GetPayload32(v)) +} + +func FromInt16(v int16) Value { + meta := BuildMeta(TagSint16, false, false, false) + return MakeValue(meta, uint32(int32(v))) +} + +func ToInt16(v Value) int16 { + return int16(int32(GetPayload32(v))) +} + +func FromUint16(v uint16) Value { + meta := BuildMeta(TagUint16, false, false, false) + return MakeValue(meta, uint32(v)) +} + +func ToUint16(v Value) uint16 { + return uint16(GetPayload32(v)) +} + +func FromInt32(v int32) Value { + meta := BuildMeta(TagSint32, false, false, false) + return MakeValue(meta, uint32(v)) +} + +func ToInt32(v Value) int32 { + return int32(GetPayload32(v)) +} + +func FromUint32(v uint32) Value { + meta := BuildMeta(TagUint32, false, false, false) + return MakeValue(meta, v) +} + +func ToUint32(v Value) uint32 { + return GetPayload32(v) +} + +func FromFloat32(f float32) Value { + payload := math.Float32bits(f) + meta := BuildMeta(TagFloat32, false, false, false) + return MakeValue(meta, payload) +} + +func ToFloat32(v Value) float32 { + return math.Float32frombits(GetPayload32(v)) +} + +func Null() Value { + return MakeValue(0, 0) +} + +// ---- Address-based ---- + +func allocContainer(size uint32) (*baseContainer, []byte) { + buf := make([]byte, 8+size) + c := (*baseContainer)(unsafe.Pointer(&buf[0])) + c.cap = size + c.size = 0 + return c, buf[8:] +} + +func MakeString(s string, freeFlag bool) Value { + c, buf := allocContainer(uint32(len(s))) + c.size = uint32(len(s)) + if len(s) > 0 { + copy(buf, s) + } + ptr := unsafe.Pointer(c) + retain(ptr, c) + return FromAddress(ptr, TagString, freeFlag) +} + +func MakeBytes(b []byte, freeFlag bool) Value { + c, buf := allocContainer(uint32(len(b))) + c.size = uint32(len(b)) + if len(b) > 0 { + copy(buf, b) + } + ptr := unsafe.Pointer(c) + retain(ptr, c) + return FromAddress(ptr, TagBytes, freeFlag) +} + +func MakeMsgpack(b []byte, freeFlag bool) Value { + c, buf := allocContainer(uint32(len(b))) + c.size = uint32(len(b)) + if len(b) > 0 { + copy(buf, b) + } + ptr := unsafe.Pointer(c) + retain(ptr, c) + return FromAddress(ptr, TagMsgpack, freeFlag) +} + +func MakeError(msg string) Value { + c, buf := allocContainer(uint32(len(msg))) + c.size = uint32(len(msg)) + if len(msg) > 0 { + copy(buf, msg) + } + ptr := unsafe.Pointer(c) + retain(ptr, c) + return FromAddress(ptr, TagError, true) +} + +func MakeFloat64(f float64, freeFlag bool) Value { + c := new(float64Container) + c.v = f + ptr := unsafe.Pointer(c) + retain(ptr, c) + return FromAddress(ptr, TagFloat64, freeFlag) +} + +// ---- Converters ---- + +func ToString(v Value, allowFree bool) string { + if !IsAddress(v) || GetTag(v) != TagString { + panic("walink: expected address-based STRING tag") + } + + payload := getPointer(v, allowFree) + if payload == nullPointer { + return "" + } + + c := (*baseContainer)(payload) + + var res string + if c.size > 0 { + res = string(unsafe.Slice((*byte)(unsafe.Pointer(&c.data)), c.size)) + } + + if allowFree && (GetMeta(v)&MetaFreeFlag) != 0 { + WalinkFree(v) + } + return res +} + +func ToBytes(v Value, allowFree bool) []byte { + if !IsAddress(v) || GetTag(v) != TagBytes { + panic("walink: expected address-based BYTES tag") + } + + payload := getPointer(v, allowFree) + if payload == nullPointer { + return nil + } + + c := (*baseContainer)(payload) + + res := make([]byte, c.size) + if c.size > 0 { + copy(res, unsafe.Slice((*byte)(unsafe.Pointer(&c.data)), c.size)) + } + if allowFree && (GetMeta(v)&MetaFreeFlag) != 0 { + WalinkFree(v) + } + return res +} + +func ToMsgpackBytes(v Value, allowFree bool) []byte { + if !IsAddress(v) || GetTag(v) != TagMsgpack { + panic("walink: expected address-based MSGPACK tag") + } + + payload := getPointer(v, allowFree) + if payload == nullPointer { + return nil + } + + c := (*baseContainer)(payload) + + res := make([]byte, c.size) + if c.size > 0 { + copy(res, unsafe.Slice((*byte)(unsafe.Pointer(&c.data)), c.size)) + } + if allowFree && (GetMeta(v)&MetaFreeFlag) != 0 { + WalinkFree(v) + } + return res +} + +func ToFloat64(v Value, allowFree bool) float64 { + if !IsAddress(v) || GetTag(v) != TagFloat64 { + panic("walink: expected address-based FLOAT64 tag") + } + + payload := getPointer(v, allowFree) + if payload == nullPointer { + return 0 + } + + c := (*float64Container)(payload) + + res := c.v + if allowFree && (GetMeta(v)&MetaFreeFlag) != 0 { + WalinkFree(v) + } + return res +} + +// ---- Exported C API ---- + +//export walink_alloc +//go:wasmexport walink_alloc +func WalinkAlloc(size uint32) Value { + buf := make([]byte, size) + ptr := unsafe.Pointer(&buf[0]) + retain(ptr, buf) + return MakeValue(0, uint32(uintptr(ptr))) +} + +//export walink_free +//go:wasmexport walink_free +func WalinkFree(v Value) Value { + if !IsAddress(v) { + return FromBool(false) + } + payload := GetPayload32(v) + release(unsafe.Pointer(uintptr(payload)), true) + return FromBool(true) +} diff --git a/go/wlvalue/walink_test.go b/go/wlvalue/walink_test.go new file mode 100644 index 0000000..7ca1630 --- /dev/null +++ b/go/wlvalue/walink_test.go @@ -0,0 +1,52 @@ +package wlvalue + +import ( + "testing" +) + +func TestValueLayout(t *testing.T) { + v := FromInt32(123) + if GetTag(v) != TagSint32 { + t.Errorf("Expected TagSint32, got %v", GetTag(v)) + } + if ToInt32(v) != 123 { + t.Errorf("Expected 123, got %v", ToInt32(v)) + } + + b := FromBool(true) + if GetTag(b) != TagBoolean { + t.Errorf("Expected TagBoolean, got %v", GetTag(b)) + } + if GetPayload32(b) != 1 { + t.Errorf("Expected payload 1, got %v", GetPayload32(b)) + } +} + +func TestString(t *testing.T) { + s := "Hello Walink" + v := MakeString(s, false) + if GetTag(v) != TagString { + t.Errorf("Expected TagString, got %v", GetTag(v)) + } + if !IsAddress(v) { + t.Errorf("Expected address-based value") + } + + res := ToString(v, false) + if res != s { + t.Errorf("Expected %s, got %s", s, res) + } +} + +func TestFloat64(t *testing.T) { + f := 3.1415926535 + v := MakeFloat64(f, false) + if GetTag(v) != TagFloat64 { + t.Errorf("Expected TagFloat64, got %v", GetTag(v)) + } + + res := ToFloat64(v, false) + if res != f { + t.Errorf("Expected %v, got %v", f, res) + } +} diff --git a/node/.gitignore b/node/.gitignore index 0ccb8df..e30f81a 100644 --- a/node/.gitignore +++ b/node/.gitignore @@ -139,3 +139,5 @@ dist vite.config.js.timestamp-* vite.config.ts.timestamp-* .vite/ + +*.wasm \ No newline at end of file diff --git a/node/src/walink.ts b/node/src/walink.ts index 3eab167..fa848a8 100644 --- a/node/src/walink.ts +++ b/node/src/walink.ts @@ -1,33 +1,32 @@ import { - type WlValue, - type WlAddress, - WlTag, - hasFreeFlag, - getTag, - getValueOrAddr, - toBool, - toSint8, - toUint8, - toSint16, - toUint16, - toSint32, - toUint32, - toFloat32, fromBool, - fromSint8, - fromUint8, + fromFloat32, fromSint16, + fromSint32, + fromSint8, fromUint16, fromUint32, - fromFloat32, - fromSint32, + fromUint8, + getAddress, + getTag, + getValueOrAddr, + hasFreeFlag, makeMeta, makeValue, - getAddress, + toBool, + toFloat32, + toSint16, + toSint32, + toSint8, + toUint16, + toUint32, + toUint8, + type WlAddress, + WlTag, + type WlValue, } from './wlvalue'; -import { pack, unpack } from 'msgpackr'; -import {TextEncoder} from "util"; +import {pack, unpack} from 'msgpackr'; const BaseContainerSize = 8; @@ -117,10 +116,54 @@ export interface WalinkCoreExports { walink_alloc(size: number): WlValue; // WL_VALUE walink_free(WL_VALUE value); walink_free(value: WlValue): WlValue; + // WL_VALUE (WL_VALUE v, uint32 argsPtr, uint32 argsLen) Value + walink_callback(value: WlValue, argsPtr: number, argsLen: number): WlValue; } export interface WalinkOptions { exports: WalinkCoreExports; + memory: WebAssembly.Memory; +} + +export class WalinkHost { + private readonly callbacks: Record any> = {}; + private callbackSeq: number = 1; + private _walink!: Walink; + + public createWalinkFromInstance(instance: WebAssembly.Instance, options?: Partial) { + const exports = options?.exports || instance.exports as unknown as WalinkCoreExports; + const memory = options?.memory || exports.memory; + if (!(memory instanceof WebAssembly.Memory)) { + throw new Error('walink: wasm memory must be a WebAssembly.Memory'); + } + const o = new Walink(this, { + exports: exports, + memory: memory, + }); + this._walink = o; + return o; + } + + public goCallbackHandler(v: WlValue, argsPtr: number, argsLen: number): WlValue { + const tag = getTag(v); + if (tag != WlTag.CALLBACK) { + throw new Error('is not callback'); + } + const callbackId = getValueOrAddr(v); + const callbackFn = this.callbacks[callbackId]; + if (!callbackFn) { + throw new Error('no exists callback function'); + } + + return this._walink.goCallbackHandler(callbackFn, argsPtr, argsLen); + } + + public registerCallback(cb: (...args: any[]) => any): WlValue { + const meta = makeMeta(WlTag.CALLBACK, false, false, false); + const callbackId = this.callbackSeq++; + this.callbacks[callbackId] = cb; + return makeValue(meta, callbackId); + } } // Core Walink runtime: generic WL_VALUE helpers bound to a wasm instance. @@ -130,9 +173,12 @@ export class Walink { private readonly textEncoder: TextEncoder; private readonly textDecoder: TextDecoder; - constructor(options: WalinkOptions) { + constructor( + private readonly host: WalinkHost, + options: WalinkOptions + ) { this.exports = options.exports; - this.memory = options.exports.memory; + this.memory = options.memory; this.textEncoder = new TextEncoder(); this.textDecoder = new TextDecoder('utf-8'); } @@ -384,13 +430,22 @@ export class Walink { return value; // raw for unsupported } } + + public registerCallback(cb: (...args: any[]) => any): WlValue { + return this.host.registerCallback(cb); + } + + public goCallbackHandler(callbackFn: (...args: any[]) => any, argsPtr: number, argsLen: number) { + const view = new BigUint64Array(this.memory.buffer, argsPtr, argsLen); + const args: WlValue[] = []; + for (let i=0; i { + const wasmPath = path.resolve(__dirname, './go-integration-test.wasm'); + const wasmBytes = await readFile(wasmPath); + + const go = new Go(); + // Go WASM expects walink_callback in 'env' + go.importObject.env = { + ...go.importObject.env, + walink_callback: (v: bigint, argsPtr: number, argsLen: number) => { + return runtime.goCallbackHandler(v, argsPtr, argsLen); + } + }; + + const result = await WebAssembly.instantiate(wasmBytes, go.importObject); + go.run(result.instance); // Start Go runtime + return { instance: result.instance, go }; +} + +describe('Go WASM integration', () => { + let walink: Walink; + let runtime: WalinkHost; + + beforeAll(async () => { + runtime = new WalinkHost(); + const { instance } = await loadGoWasmInstance(runtime); + console.error('instance : ', instance.exports); + walink = runtime.createWalinkFromInstance(instance, { + memory: instance.exports['mem'] as any, + }); + }); + + it('adds numbers through Go WASM', () => { + const exports = (walink as any).exports; + const a = walink.toWlSint32(10); + const b = walink.toWlSint32(20); + const result = exports.add(a, b); + expect(walink.fromWlSint32(result)).toBe(30); + }); + + it('echoes string through Go WASM', () => { + const exports = (walink as any).exports; + const input = 'Hello from Node.js'; + const vInput = walink.toWlString(input); + const vResult = exports.echo_string(vInput); + expect(walink.fromWlString(vResult)).toBe(input); + }); + + it('calls back to Node.js from Go WASM', () => { + const exports = (walink as any).exports; + let called = false; + let receivedVal: any = null; + + const cb = walink.registerCallback((v: bigint) => { + called = true; + receivedVal = walink.decode(v); + return walink.toWlSint32(123); + }); + + const inputVal = walink.toWlSint32(456); + const result = exports.call_me_back(cb, inputVal); + + expect(called).toBe(true); + expect(receivedVal).toBe(456); + expect(walink.fromWlSint32(result)).toBe(123); + }); +}); diff --git a/node/test/walink.integration.test.ts b/node/test/walink.integration.test.ts index f9ef959..4afa4a8 100644 --- a/node/test/walink.integration.test.ts +++ b/node/test/walink.integration.test.ts @@ -1,16 +1,16 @@ -import { readFile } from "node:fs/promises"; -import path from "node:path"; -import { fileURLToPath } from "node:url"; +import { readFile } from 'node:fs/promises'; +import * as path from 'node:path'; +import { fileURLToPath } from 'node:url'; -import { beforeAll, describe, expect, it } from "vitest"; +import { beforeAll, describe, expect, it } from 'vitest'; -import { createWalinkWithSampleApi, WalinkWithSampleApi } from "./walinkSampleApi"; +import { createWalinkWithSampleApi, WalinkWithSampleApi } from './walinkSampleApi'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function loadWasmInstance(): Promise { - const wasmPath = path.resolve(__dirname, "../../cpp/build/walink_test.wasm"); + const wasmPath = path.resolve(__dirname, '../../cpp/build/walink_test.wasm'); const bytes = await readFile(wasmPath); const wasmImports = { @@ -25,7 +25,7 @@ async function loadWasmInstance(): Promise { return result.instance; } -describe("walink wasm integration", () => { +describe('walink wasm integration', () => { let walink: WalinkWithSampleApi; beforeAll(async () => { @@ -33,23 +33,23 @@ describe("walink wasm integration", () => { walink = createWalinkWithSampleApi(instance); }); - it("roundtrips boolean values", () => { + it('roundtrips boolean values', () => { expect(walink.roundtripBool(true)).toBe(true); expect(walink.roundtripBool(false)).toBe(false); }); - it("adds sint32 values", () => { + it('adds sint32 values', () => { expect(walink.addSint32(1, 2)).toBe(3); expect(walink.addSint32(-10, 5)).toBe(-5); }); - it("creates hello string from wasm", () => { + it('creates hello string from wasm', () => { const str = walink.makeHelloString(); - expect(str).toBe("hello from wasm"); + expect(str).toBe('hello from wasm'); }); - it("echoes hello string through wasm", () => { + it('echoes hello string through wasm', () => { const str = walink.echoHelloString(); - expect(str).toBe("hello from wasm"); + expect(str).toBe('hello from wasm'); }); }); \ No newline at end of file diff --git a/node/test/walinkSampleApi.ts b/node/test/walinkSampleApi.ts index c7121df..2ac10d4 100644 --- a/node/test/walinkSampleApi.ts +++ b/node/test/walinkSampleApi.ts @@ -1,10 +1,8 @@ import { - type WlTag, type WlValue, - createWalinkFromInstance, Walink, - WalinkCoreExports, -} from "../src"; + WalinkCoreExports, WalinkHost, +} from '../src'; // wasm 테스트 모듈이 export 하는 테스트용 C API 시그니처 export interface WalinkTestExports extends WalinkCoreExports { @@ -18,8 +16,11 @@ export interface WalinkTestExports extends WalinkCoreExports { export class WalinkWithSampleApi extends Walink { protected readonly testExports: WalinkTestExports; - constructor(exports: WalinkTestExports) { - super({ exports }); + constructor(exports: any) { + super(new WalinkHost(), { + exports: exports, + memory: exports.memory, + }); this.testExports = exports; } @@ -55,9 +56,7 @@ export function createWalinkWithSampleApi( ): WalinkWithSampleApi { const exports = instance.exports as unknown as WalinkTestExports; if (!(exports.memory instanceof WebAssembly.Memory)) { - throw new Error("walink: wasm instance.exports.memory must be a WebAssembly.Memory"); + throw new Error('walink: wasm instance.exports.memory must be a WebAssembly.Memory'); } - // walink_free 가 없는 경우도 방어적으로 체크할 수 있지만, - // 현재 테스트 wasm 모듈은 항상 export 한다고 가정. return new WalinkWithSampleApi(exports); } \ No newline at end of file diff --git a/node/test/wasm_exec.js b/node/test/wasm_exec.js new file mode 100644 index 0000000..d71af9e --- /dev/null +++ b/node/test/wasm_exec.js @@ -0,0 +1,575 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +"use strict"; + +(() => { + const enosys = () => { + const err = new Error("not implemented"); + err.code = "ENOSYS"; + return err; + }; + + if (!globalThis.fs) { + let outputBuf = ""; + globalThis.fs = { + constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1, O_DIRECTORY: -1 }, // unused + writeSync(fd, buf) { + outputBuf += decoder.decode(buf); + const nl = outputBuf.lastIndexOf("\n"); + if (nl != -1) { + console.log(outputBuf.substring(0, nl)); + outputBuf = outputBuf.substring(nl + 1); + } + return buf.length; + }, + write(fd, buf, offset, length, position, callback) { + if (offset !== 0 || length !== buf.length || position !== null) { + callback(enosys()); + return; + } + const n = this.writeSync(fd, buf); + callback(null, n); + }, + chmod(path, mode, callback) { callback(enosys()); }, + chown(path, uid, gid, callback) { callback(enosys()); }, + close(fd, callback) { callback(enosys()); }, + fchmod(fd, mode, callback) { callback(enosys()); }, + fchown(fd, uid, gid, callback) { callback(enosys()); }, + fstat(fd, callback) { callback(enosys()); }, + fsync(fd, callback) { callback(null); }, + ftruncate(fd, length, callback) { callback(enosys()); }, + lchown(path, uid, gid, callback) { callback(enosys()); }, + link(path, link, callback) { callback(enosys()); }, + lstat(path, callback) { callback(enosys()); }, + mkdir(path, perm, callback) { callback(enosys()); }, + open(path, flags, mode, callback) { callback(enosys()); }, + read(fd, buffer, offset, length, position, callback) { callback(enosys()); }, + readdir(path, callback) { callback(enosys()); }, + readlink(path, callback) { callback(enosys()); }, + rename(from, to, callback) { callback(enosys()); }, + rmdir(path, callback) { callback(enosys()); }, + stat(path, callback) { callback(enosys()); }, + symlink(path, link, callback) { callback(enosys()); }, + truncate(path, length, callback) { callback(enosys()); }, + unlink(path, callback) { callback(enosys()); }, + utimes(path, atime, mtime, callback) { callback(enosys()); }, + }; + } + + if (!globalThis.process) { + globalThis.process = { + getuid() { return -1; }, + getgid() { return -1; }, + geteuid() { return -1; }, + getegid() { return -1; }, + getgroups() { throw enosys(); }, + pid: -1, + ppid: -1, + umask() { throw enosys(); }, + cwd() { throw enosys(); }, + chdir() { throw enosys(); }, + } + } + + if (!globalThis.path) { + globalThis.path = { + resolve(...pathSegments) { + return pathSegments.join("/"); + } + } + } + + if (!globalThis.crypto) { + throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"); + } + + if (!globalThis.performance) { + throw new Error("globalThis.performance is not available, polyfill required (performance.now only)"); + } + + if (!globalThis.TextEncoder) { + throw new Error("globalThis.TextEncoder is not available, polyfill required"); + } + + if (!globalThis.TextDecoder) { + throw new Error("globalThis.TextDecoder is not available, polyfill required"); + } + + const encoder = new TextEncoder("utf-8"); + const decoder = new TextDecoder("utf-8"); + + globalThis.Go = class { + constructor() { + this.argv = ["js"]; + this.env = {}; + this.exit = (code) => { + if (code !== 0) { + console.warn("exit code:", code); + } + }; + this._exitPromise = new Promise((resolve) => { + this._resolveExitPromise = resolve; + }); + this._pendingEvent = null; + this._scheduledTimeouts = new Map(); + this._nextCallbackTimeoutID = 1; + + const setInt64 = (addr, v) => { + this.mem.setUint32(addr + 0, v, true); + this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true); + } + + const setInt32 = (addr, v) => { + this.mem.setUint32(addr + 0, v, true); + } + + const getInt64 = (addr) => { + const low = this.mem.getUint32(addr + 0, true); + const high = this.mem.getInt32(addr + 4, true); + return low + high * 4294967296; + } + + const loadValue = (addr) => { + const f = this.mem.getFloat64(addr, true); + if (f === 0) { + return undefined; + } + if (!isNaN(f)) { + return f; + } + + const id = this.mem.getUint32(addr, true); + return this._values[id]; + } + + const storeValue = (addr, v) => { + const nanHead = 0x7FF80000; + + if (typeof v === "number" && v !== 0) { + if (isNaN(v)) { + this.mem.setUint32(addr + 4, nanHead, true); + this.mem.setUint32(addr, 0, true); + return; + } + this.mem.setFloat64(addr, v, true); + return; + } + + if (v === undefined) { + this.mem.setFloat64(addr, 0, true); + return; + } + + let id = this._ids.get(v); + if (id === undefined) { + id = this._idPool.pop(); + if (id === undefined) { + id = this._values.length; + } + this._values[id] = v; + this._goRefCounts[id] = 0; + this._ids.set(v, id); + } + this._goRefCounts[id]++; + let typeFlag = 0; + switch (typeof v) { + case "object": + if (v !== null) { + typeFlag = 1; + } + break; + case "string": + typeFlag = 2; + break; + case "symbol": + typeFlag = 3; + break; + case "function": + typeFlag = 4; + break; + } + this.mem.setUint32(addr + 4, nanHead | typeFlag, true); + this.mem.setUint32(addr, id, true); + } + + const loadSlice = (addr) => { + const array = getInt64(addr + 0); + const len = getInt64(addr + 8); + return new Uint8Array(this._inst.exports.mem.buffer, array, len); + } + + const loadSliceOfValues = (addr) => { + const array = getInt64(addr + 0); + const len = getInt64(addr + 8); + const a = new Array(len); + for (let i = 0; i < len; i++) { + a[i] = loadValue(array + i * 8); + } + return a; + } + + const loadString = (addr) => { + const saddr = getInt64(addr + 0); + const len = getInt64(addr + 8); + return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len)); + } + + const testCallExport = (a, b) => { + this._inst.exports.testExport0(); + return this._inst.exports.testExport(a, b); + } + + const timeOrigin = Date.now() - performance.now(); + this.importObject = { + _gotest: { + add: (a, b) => a + b, + callExport: testCallExport, + }, + gojs: { + // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters) + // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported + // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function). + // This changes the SP, thus we have to update the SP used by the imported function. + + // func wasmExit(code int32) + "runtime.wasmExit": (sp) => { + sp >>>= 0; + const code = this.mem.getInt32(sp + 8, true); + this.exited = true; + delete this._inst; + delete this._values; + delete this._goRefCounts; + delete this._ids; + delete this._idPool; + this.exit(code); + }, + + // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32) + "runtime.wasmWrite": (sp) => { + sp >>>= 0; + const fd = getInt64(sp + 8); + const p = getInt64(sp + 16); + const n = this.mem.getInt32(sp + 24, true); + fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n)); + }, + + // func resetMemoryDataView() + "runtime.resetMemoryDataView": (sp) => { + sp >>>= 0; + this.mem = new DataView(this._inst.exports.mem.buffer); + }, + + // func nanotime1() int64 + "runtime.nanotime1": (sp) => { + sp >>>= 0; + setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000); + }, + + // func walltime() (sec int64, nsec int32) + "runtime.walltime": (sp) => { + sp >>>= 0; + const msec = (new Date).getTime(); + setInt64(sp + 8, msec / 1000); + this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true); + }, + + // func scheduleTimeoutEvent(delay int64) int32 + "runtime.scheduleTimeoutEvent": (sp) => { + sp >>>= 0; + const id = this._nextCallbackTimeoutID; + this._nextCallbackTimeoutID++; + this._scheduledTimeouts.set(id, setTimeout( + () => { + this._resume(); + while (this._scheduledTimeouts.has(id)) { + // for some reason Go failed to register the timeout event, log and try again + // (temporary workaround for https://github.com/golang/go/issues/28975) + console.warn("scheduleTimeoutEvent: missed timeout event"); + this._resume(); + } + }, + getInt64(sp + 8), + )); + this.mem.setInt32(sp + 16, id, true); + }, + + // func clearTimeoutEvent(id int32) + "runtime.clearTimeoutEvent": (sp) => { + sp >>>= 0; + const id = this.mem.getInt32(sp + 8, true); + clearTimeout(this._scheduledTimeouts.get(id)); + this._scheduledTimeouts.delete(id); + }, + + // func getRandomData(r []byte) + "runtime.getRandomData": (sp) => { + sp >>>= 0; + crypto.getRandomValues(loadSlice(sp + 8)); + }, + + // func finalizeRef(v ref) + "syscall/js.finalizeRef": (sp) => { + sp >>>= 0; + const id = this.mem.getUint32(sp + 8, true); + this._goRefCounts[id]--; + if (this._goRefCounts[id] === 0) { + const v = this._values[id]; + this._values[id] = null; + this._ids.delete(v); + this._idPool.push(id); + } + }, + + // func stringVal(value string) ref + "syscall/js.stringVal": (sp) => { + sp >>>= 0; + storeValue(sp + 24, loadString(sp + 8)); + }, + + // func valueGet(v ref, p string) ref + "syscall/js.valueGet": (sp) => { + sp >>>= 0; + const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16)); + sp = this._inst.exports.getsp() >>> 0; // see comment above + storeValue(sp + 32, result); + }, + + // func valueSet(v ref, p string, x ref) + "syscall/js.valueSet": (sp) => { + sp >>>= 0; + Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32)); + }, + + // func valueDelete(v ref, p string) + "syscall/js.valueDelete": (sp) => { + sp >>>= 0; + Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16)); + }, + + // func valueIndex(v ref, i int) ref + "syscall/js.valueIndex": (sp) => { + sp >>>= 0; + storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16))); + }, + + // valueSetIndex(v ref, i int, x ref) + "syscall/js.valueSetIndex": (sp) => { + sp >>>= 0; + Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24)); + }, + + // func valueCall(v ref, m string, args []ref) (ref, bool) + "syscall/js.valueCall": (sp) => { + sp >>>= 0; + try { + const v = loadValue(sp + 8); + const m = Reflect.get(v, loadString(sp + 16)); + const args = loadSliceOfValues(sp + 32); + const result = Reflect.apply(m, v, args); + sp = this._inst.exports.getsp() >>> 0; // see comment above + storeValue(sp + 56, result); + this.mem.setUint8(sp + 64, 1); + } catch (err) { + sp = this._inst.exports.getsp() >>> 0; // see comment above + storeValue(sp + 56, err); + this.mem.setUint8(sp + 64, 0); + } + }, + + // func valueInvoke(v ref, args []ref) (ref, bool) + "syscall/js.valueInvoke": (sp) => { + sp >>>= 0; + try { + const v = loadValue(sp + 8); + const args = loadSliceOfValues(sp + 16); + const result = Reflect.apply(v, undefined, args); + sp = this._inst.exports.getsp() >>> 0; // see comment above + storeValue(sp + 40, result); + this.mem.setUint8(sp + 48, 1); + } catch (err) { + sp = this._inst.exports.getsp() >>> 0; // see comment above + storeValue(sp + 40, err); + this.mem.setUint8(sp + 48, 0); + } + }, + + // func valueNew(v ref, args []ref) (ref, bool) + "syscall/js.valueNew": (sp) => { + sp >>>= 0; + try { + const v = loadValue(sp + 8); + const args = loadSliceOfValues(sp + 16); + const result = Reflect.construct(v, args); + sp = this._inst.exports.getsp() >>> 0; // see comment above + storeValue(sp + 40, result); + this.mem.setUint8(sp + 48, 1); + } catch (err) { + sp = this._inst.exports.getsp() >>> 0; // see comment above + storeValue(sp + 40, err); + this.mem.setUint8(sp + 48, 0); + } + }, + + // func valueLength(v ref) int + "syscall/js.valueLength": (sp) => { + sp >>>= 0; + setInt64(sp + 16, parseInt(loadValue(sp + 8).length)); + }, + + // valuePrepareString(v ref) (ref, int) + "syscall/js.valuePrepareString": (sp) => { + sp >>>= 0; + const str = encoder.encode(String(loadValue(sp + 8))); + storeValue(sp + 16, str); + setInt64(sp + 24, str.length); + }, + + // valueLoadString(v ref, b []byte) + "syscall/js.valueLoadString": (sp) => { + sp >>>= 0; + const str = loadValue(sp + 8); + loadSlice(sp + 16).set(str); + }, + + // func valueInstanceOf(v ref, t ref) bool + "syscall/js.valueInstanceOf": (sp) => { + sp >>>= 0; + this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0); + }, + + // func copyBytesToGo(dst []byte, src ref) (int, bool) + "syscall/js.copyBytesToGo": (sp) => { + sp >>>= 0; + const dst = loadSlice(sp + 8); + const src = loadValue(sp + 32); + if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) { + this.mem.setUint8(sp + 48, 0); + return; + } + const toCopy = src.subarray(0, dst.length); + dst.set(toCopy); + setInt64(sp + 40, toCopy.length); + this.mem.setUint8(sp + 48, 1); + }, + + // func copyBytesToJS(dst ref, src []byte) (int, bool) + "syscall/js.copyBytesToJS": (sp) => { + sp >>>= 0; + const dst = loadValue(sp + 8); + const src = loadSlice(sp + 16); + if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) { + this.mem.setUint8(sp + 48, 0); + return; + } + const toCopy = src.subarray(0, dst.length); + dst.set(toCopy); + setInt64(sp + 40, toCopy.length); + this.mem.setUint8(sp + 48, 1); + }, + + "debug": (value) => { + console.log(value); + }, + } + }; + } + + async run(instance) { + if (!(instance instanceof WebAssembly.Instance)) { + throw new Error("Go.run: WebAssembly.Instance expected"); + } + this._inst = instance; + this.mem = new DataView(this._inst.exports.mem.buffer); + this._values = [ // JS values that Go currently has references to, indexed by reference id + NaN, + 0, + null, + true, + false, + globalThis, + this, + ]; + this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id + this._ids = new Map([ // mapping from JS values to reference ids + [0, 1], + [null, 2], + [true, 3], + [false, 4], + [globalThis, 5], + [this, 6], + ]); + this._idPool = []; // unused ids that have been garbage collected + this.exited = false; // whether the Go program has exited + + // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory. + let offset = 4096; + + const strPtr = (str) => { + const ptr = offset; + const bytes = encoder.encode(str + "\0"); + new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes); + offset += bytes.length; + if (offset % 8 !== 0) { + offset += 8 - (offset % 8); + } + return ptr; + }; + + const argc = this.argv.length; + + const argvPtrs = []; + this.argv.forEach((arg) => { + argvPtrs.push(strPtr(arg)); + }); + argvPtrs.push(0); + + const keys = Object.keys(this.env).sort(); + keys.forEach((key) => { + argvPtrs.push(strPtr(`${key}=${this.env[key]}`)); + }); + argvPtrs.push(0); + + const argv = offset; + argvPtrs.forEach((ptr) => { + this.mem.setUint32(offset, ptr, true); + this.mem.setUint32(offset + 4, 0, true); + offset += 8; + }); + + // The linker guarantees global data starts from at least wasmMinDataAddr. + // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr. + const wasmMinDataAddr = 4096 + 8192; + if (offset >= wasmMinDataAddr) { + throw new Error("total length of command line and environment variables exceeds limit"); + } + + this._inst.exports.run(argc, argv); + if (this.exited) { + this._resolveExitPromise(); + } + await this._exitPromise; + } + + _resume() { + if (this.exited) { + throw new Error("Go program has already exited"); + } + this._inst.exports.resume(); + if (this.exited) { + this._resolveExitPromise(); + } + } + + _makeFuncWrapper(id) { + const go = this; + return function () { + const event = { id: id, this: this, args: arguments }; + go._pendingEvent = event; + go._resume(); + return event.result; + }; + } + } +})();