Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix js str decode #727

Merged
merged 1 commit into from
Nov 9, 2024
Merged
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
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';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

my editor reformatted some stuff. I hope this is okay.

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()"#,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as i could see there isn't actually a DiplomatSliceStrings in the runtime, but I thought consistency would be a good guide

match encoding {
hir::StringEncoding::Utf8
| hir::StringEncoding::UnvalidatedUtf8 => 8,
Expand Down