From 7a2fbdc3f4dd0912bef8a1069d224afc09ae5d10 Mon Sep 17 00:00:00 2001 From: jcrist1 Date: Tue, 5 Nov 2024 02:59:05 +0100 Subject: [PATCH] Fix primitive slice js (#722) * fix-primitive-slice-js * remove bench code * gen example js * fix primitive slice test * Update feature_tests/js/test/slices.mjs Co-authored-by: Tyler K --------- Co-authored-by: Manish Goregaokar Co-authored-by: Tyler K --- example/js/lib/api/diplomat-runtime.mjs | 2 +- feature_tests/js/api/Float64Vec.mjs | 4 ++-- feature_tests/js/api/diplomat-runtime.mjs | 2 +- feature_tests/js/test/slices-ts.mjs | 9 +++++++-- feature_tests/js/test/slices-ts.mts | 21 ++++++++++++++------- feature_tests/js/test/slices.mjs | 10 ++++++++-- tool/src/js/converter.rs | 2 +- tool/templates/js/runtime.mjs | 2 +- 8 files changed, 35 insertions(+), 17 deletions(-) diff --git a/example/js/lib/api/diplomat-runtime.mjs b/example/js/lib/api/diplomat-runtime.mjs index 18846dfc0..fc8d591f3 100644 --- a/example/js/lib/api/diplomat-runtime.mjs +++ b/example/js/lib/api/diplomat-runtime.mjs @@ -606,4 +606,4 @@ export class GarbageCollectorGrip { } } -const DiplomatBufferFinalizer = new FinalizationRegistry(free => free()); \ No newline at end of file +const DiplomatBufferFinalizer = new FinalizationRegistry(free => free()); diff --git a/feature_tests/js/api/Float64Vec.mjs b/feature_tests/js/api/Float64Vec.mjs index 558ed2699..bac5ff7e5 100644 --- a/feature_tests/js/api/Float64Vec.mjs +++ b/feature_tests/js/api/Float64Vec.mjs @@ -154,7 +154,7 @@ export class Float64Vec { const result = wasm.Float64Vec_as_slice(diplomatReceive.buffer, this.ffiValue); try { - return new diplomatRuntime.DiplomatSlicePrimitive.getSlice(wasm, diplomatReceive.buffer, "f64", aEdges); + return Array.from(new diplomatRuntime.DiplomatSlicePrimitive(wasm, diplomatReceive.buffer, "f64", aEdges).getValue()); } finally { @@ -210,7 +210,7 @@ export class Float64Vec { const result = wasm.Float64Vec_borrow(diplomatReceive.buffer, this.ffiValue); try { - return new diplomatRuntime.DiplomatSlicePrimitive.getSlice(wasm, diplomatReceive.buffer, "f64", aEdges); + return Array.from(new diplomatRuntime.DiplomatSlicePrimitive(wasm, diplomatReceive.buffer, "f64", aEdges).getValue()); } finally { diff --git a/feature_tests/js/api/diplomat-runtime.mjs b/feature_tests/js/api/diplomat-runtime.mjs index 18846dfc0..fc8d591f3 100644 --- a/feature_tests/js/api/diplomat-runtime.mjs +++ b/feature_tests/js/api/diplomat-runtime.mjs @@ -606,4 +606,4 @@ export class GarbageCollectorGrip { } } -const DiplomatBufferFinalizer = new FinalizationRegistry(free => free()); \ No newline at end of file +const DiplomatBufferFinalizer = new FinalizationRegistry(free => free()); diff --git a/feature_tests/js/test/slices-ts.mjs b/feature_tests/js/test/slices-ts.mjs index ab517dced..df199fccb 100644 --- a/feature_tests/js/test/slices-ts.mjs +++ b/feature_tests/js/test/slices-ts.mjs @@ -1,5 +1,5 @@ -import test from 'ava'; -import { MyString } from "diplomat-wasm-js-feature-tests"; +import test from "ava"; +import { MyString, Float64Vec } from "diplomat-wasm-js-feature-tests"; test("MyString functionality", (t) => { let str = MyString.new_("This is a test value."); t.is(str.str, "This is a test value."); @@ -8,3 +8,8 @@ test("String List", (t) => { let str = MyString.newFromFirst(["This", "is", "a", "test."]); t.is(str.str, "This"); }); +test("Float64Vec", (t) => { + let input = [1, 2, 3, 4, 5]; + let data = Float64Vec.newIsize(input); + t.deepEqual(data.borrow(), input); +}); diff --git a/feature_tests/js/test/slices-ts.mts b/feature_tests/js/test/slices-ts.mts index dbeeee625..b67ab7551 100644 --- a/feature_tests/js/test/slices-ts.mts +++ b/feature_tests/js/test/slices-ts.mts @@ -1,12 +1,19 @@ -import test from 'ava'; -import { MyString } from "diplomat-wasm-js-feature-tests"; +import test from "ava"; +import { MyString, Float64Vec } from "diplomat-wasm-js-feature-tests"; +import wasm from "../api/diplomat-wasm.mjs"; test("MyString functionality", (t) => { - let str = MyString.new_("This is a test value."); - t.is(str.str, "This is a test value."); + let str = MyString.new_("This is a test value."); + t.is(str.str, "This is a test value."); }); test("String List", (t) => { - let str = MyString.newFromFirst(["This", "is", "a", "test."]); - t.is(str.str, "This"); -}); \ No newline at end of file + let str = MyString.newFromFirst(["This", "is", "a", "test."]); + t.is(str.str, "This"); +}); + +test("Float64Vec", (t) => { + let input = [1, 2, 3, 4, 5]; + let data = Float64Vec.newIsize(input); + t.deepEqual(data.borrow(), input); +}); diff --git a/feature_tests/js/test/slices.mjs b/feature_tests/js/test/slices.mjs index dbeeee625..07f6467d3 100644 --- a/feature_tests/js/test/slices.mjs +++ b/feature_tests/js/test/slices.mjs @@ -1,5 +1,5 @@ import test from 'ava'; -import { MyString } from "diplomat-wasm-js-feature-tests"; +import { MyString, Float64Vec} from "diplomat-wasm-js-feature-tests"; test("MyString functionality", (t) => { let str = MyString.new_("This is a test value."); @@ -9,4 +9,10 @@ test("MyString functionality", (t) => { test("String List", (t) => { let str = MyString.newFromFirst(["This", "is", "a", "test."]); t.is(str.str, "This"); -}); \ No newline at end of file +}); + +test("Float64Vec", (t) => { + let input = [1, 2, 3, 4, 5]; + let data = Float64Vec.newIsize(input); + t.deepEqual(data.borrow(), input); +}); diff --git a/tool/src/js/converter.rs b/tool/src/js/converter.rs index 619047397..43151a799 100644 --- a/tool/src/js/converter.rs +++ b/tool/src/js/converter.rs @@ -239,7 +239,7 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> { // Slices are always returned to us by way of pointers, so we assume that we can just access DiplomatReceiveBuf's helper functions: match slice { hir::Slice::Primitive(_, primitive_type) => format!( - r#"new diplomatRuntime.DiplomatSlicePrimitive.getSlice(wasm, {variable_name}, "{}", {edges})"#, + r#"Array.from(new diplomatRuntime.DiplomatSlicePrimitive(wasm, {variable_name}, "{}", {edges}).getValue())"#, self.formatter.fmt_primitive_list_view(primitive_type) ) .into(), diff --git a/tool/templates/js/runtime.mjs b/tool/templates/js/runtime.mjs index 18846dfc0..fc8d591f3 100644 --- a/tool/templates/js/runtime.mjs +++ b/tool/templates/js/runtime.mjs @@ -606,4 +606,4 @@ export class GarbageCollectorGrip { } } -const DiplomatBufferFinalizer = new FinalizationRegistry(free => free()); \ No newline at end of file +const DiplomatBufferFinalizer = new FinalizationRegistry(free => free());