Skip to content

Commit

Permalink
fix js str decode (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist1 authored Nov 9, 2024
1 parent b1ae9e7 commit 6ccac64
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 21 deletions.
2 changes: 2 additions & 0 deletions feature_tests/c/include/MyString.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/cpp/include/MyString.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions feature_tests/cpp/include/MyString.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions feature_tests/dart/lib/src/MyString.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions feature_tests/js/api/BorrowedFields.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion feature_tests/js/api/BorrowedFieldsReturning.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions feature_tests/js/api/BorrowedFieldsWithBounds.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/js/api/MyString.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions feature_tests/js/api/MyString.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion feature_tests/js/api/OpaqueMutexedString.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion feature_tests/js/api/OptionString.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion feature_tests/js/api/Utf16Wrap.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions feature_tests/js/test/slices-ts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ test("String List", (t) => {
let str = MyString.newFromFirst(["This", "is", "a", "test."]);
t.is(str.str, "This");
});
test("MyString borrow", (t) => {
let str = MyString.new_("This is a test.");
t.is(str.borrow(), "This is a test.");
});
test("Float64Vec", (t) => {
let input = [1, 2, 3, 4, 5];
let data = Float64Vec.newIsize(input);
Expand Down
5 changes: 5 additions & 0 deletions feature_tests/js/test/slices-ts.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ test("String List", (t) => {
t.is(str.str, "This");
});

test("MyString borrow", (t) => {
let str = MyString.new_("This is a test.");
t.is(str.borrow(), "This is a test.");
});

test("Float64Vec", (t) => {
let input = [1, 2, 3, 4, 5];
let data = Float64Vec.newIsize(input);
Expand Down
23 changes: 14 additions & 9 deletions feature_tests/js/test/slices.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import test from 'ava';
import { MyString, Float64Vec} 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.");
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");
let str = MyString.newFromFirst(["This", "is", "a", "test."]);
t.is(str.str, "This");
});

test("MyString borrow", (t) => {
let str = MyString.new_("This is a test.");
t.is(str.borrow(), "This is a test.");
});

test("Float64Vec", (t) => {
let input = [1, 2, 3, 4, 5];
let data = Float64Vec.newIsize(input);
t.deepEqual(data.borrow(), input);
let input = [1, 2, 3, 4, 5];
let data = Float64Vec.newIsize(input);
t.deepEqual(data.borrow(), input);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions feature_tests/src/slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ mod ffi {
let _ = foo;
let _ = write;
}

pub fn borrow<'a>(&'a self) -> DiplomatStrSlice<'a> {
AsRef::<[u8]>::as_ref(&self.0).into()
}
}

#[diplomat::opaque]
Expand Down
4 changes: 2 additions & 2 deletions tool/src/js/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> {
)
.into(),
hir::Slice::Str(_, encoding) => format!(
r#"new diplomatRuntime.DiplomatSliceStr(wasm, {variable_name}, "string{}", {edges})"#,
r#"new diplomatRuntime.DiplomatSliceStr(wasm, {variable_name}, "string{}", {edges}).getValue()"#,
match encoding {
hir::StringEncoding::Utf8 | hir::StringEncoding::UnvalidatedUtf8 => 8,
hir::StringEncoding::UnvalidatedUtf16 => 16,
Expand All @@ -257,7 +257,7 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> {
// We basically iterate through and read each string into the array.
// TODO: Need a test for this.
format!(
r#"new diplomatRuntime.DiplomatSliceStrings(wasm, {variable_name}, "string{}", {edges})"#,
r#"new diplomatRuntime.DiplomatSliceStrings(wasm, {variable_name}, "string{}", {edges}).getValue()"#,
match encoding {
hir::StringEncoding::Utf8
| hir::StringEncoding::UnvalidatedUtf8 => 8,
Expand Down

0 comments on commit 6ccac64

Please sign in to comment.