-
-
Notifications
You must be signed in to change notification settings - Fork 682
feat: Add memory.dataUTF8() built-in function
#2974
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,7 @@ under the licensing terms detailed in LICENSE: | |
| * Kam Chehresa <[email protected]> | ||
| * Mopsgamer <[email protected]> | ||
| * EDM115 <[email protected]> | ||
| * Geraint Luff <[email protected]> | ||
|
|
||
| Portions of this software are derived from third-party works licensed under | ||
| the following terms: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,6 +118,9 @@ | |
| isPowerOf2 | ||
| } from "./util"; | ||
|
|
||
| // Use the built-in `TextEncoder` for UTF-8 conversion | ||
| declare let TextEncoder: any; | ||
|
Check warning on line 122 in src/builtins.ts
|
||
|
|
||
| /** Internal names of various compiler built-ins. */ | ||
| export namespace BuiltinNames { | ||
|
|
||
|
|
@@ -749,6 +752,7 @@ | |
| export const memory_copy = "~lib/memory/memory.copy"; | ||
| export const memory_fill = "~lib/memory/memory.fill"; | ||
| export const memory_data = "~lib/memory/memory.data"; | ||
| export const memory_dataUTF8 = "~lib/memory/memory.dataUTF8"; | ||
|
|
||
| // std/typedarray.ts | ||
| export const Int8Array = "~lib/typedarray/Int8Array"; | ||
|
|
@@ -3491,6 +3495,41 @@ | |
| } | ||
| builtinFunctions.set(BuiltinNames.memory_data, builtin_memory_data); | ||
|
|
||
| // memory.dataUTF8(value) -> usize | ||
| function builtin_memory_dataUTF8(ctx: BuiltinFunctionContext): ExpressionRef { | ||
| let compiler = ctx.compiler; | ||
| let module = compiler.module; | ||
| if ( | ||
| checkTypeAbsent(ctx) | | ||
| checkArgsRequired(ctx, 1) | ||
| ) return module.unreachable(); | ||
| let operands = ctx.operands; | ||
| let usizeType = compiler.options.usizeType; | ||
| let offset: i64; | ||
| let arg0 = operands[0]; | ||
| if (!arg0.isLiteralKind(LiteralKind.String)) { | ||
| compiler.error( | ||
| DiagnosticCode.String_literal_expected, | ||
| arg0.range | ||
| ); | ||
| return module.unreachable(); | ||
| } | ||
| let str = (<StringLiteralExpression>arg0).value; | ||
| let array : Uint8Array = new TextEncoder('utf8').encode(str); | ||
| let arrayNullTerminated = new Uint8Array(array.length + 1); | ||
| arrayNullTerminated.set(array); | ||
| offset = compiler.addAlignedMemorySegment(arrayNullTerminated, 1).offset; | ||
| // FIXME: what if recompiles happen? recompiles are bad. | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| compiler.currentType = usizeType; | ||
| if (usizeType == Type.usize32) { | ||
| assert(!i64_high(offset)); | ||
| return module.i32(i64_low(offset)); | ||
| } else { | ||
| return module.i64(i64_low(offset), i64_high(offset)); | ||
| } | ||
| } | ||
| builtinFunctions.set(BuiltinNames.memory_dataUTF8, builtin_memory_dataUTF8); | ||
|
|
||
| // === GC ===================================================================================== | ||
|
|
||
| function builtin_i31_new(ctx: BuiltinFunctionContext): ExpressionRef { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, this won't work for two reasons. One is is that there is no
anytype when compiling the compiler itself to Wasm, and the other is that there is no TextEncoder implementation in the standard library.What we have instead is this utility that can perhaps be reused when combined with a portable fallback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks - I'll change it to use that