mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-24 06:14:01 +08:00
Library exports and callbacks passed text only as a pointer and length. profile_format 2 adds the cstring class, a NUL-terminated `const char *` (an i32 pointer in Wasm reactors), for export parameters, export results and callback parameters. Parameters are copied in and decoded as UTF-8, with U+FFFD for malformed bytes; a NULL pointer is the SC4012 host-contract trap. Results live in the result arena like string results. A result or callback argument containing U+0000 traps instead of being truncated. Closes #615 Co-authored-by: ailuoku6 <23209876+ailuoku6@users.noreply.github.com>
27 lines
628 B
TypeScript
27 lines
628 B
TypeScript
// cstring conformance fixture: NUL-terminated text in both directions and
|
|
// through a callback channel. `withNul` and `echoNul` produce text holding
|
|
// U+0000, which a C string cannot carry.
|
|
declare function log(text: string, n: number): void;
|
|
|
|
export function greet(name: string): string {
|
|
return `hello, ${name}!`;
|
|
}
|
|
|
|
export function length(text: string): number {
|
|
return text.length;
|
|
}
|
|
|
|
export function echo(text: string): number {
|
|
log(text.toUpperCase(), text.length);
|
|
return 1;
|
|
}
|
|
|
|
export function withNul(): string {
|
|
return "a\u0000b";
|
|
}
|
|
|
|
export function echoNul(): number {
|
|
log("x\u0000y", 3);
|
|
return 1;
|
|
}
|