Skip to content

Commit f0ae1fc

Browse files
committed
Implement stringref operations
1 parent 5e3be1e commit f0ae1fc

File tree

15 files changed

+3315
-14
lines changed

15 files changed

+3315
-14
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ jobs:
7474
- uses: dcodeIO/setup-node-nvm@master
7575
with:
7676
node-mirror: https://nodejs.org/download/v8-canary/
77-
node-version: 19.0.0-v8-canary202209029fc5a9347b
77+
node-version: 21.0.0-v8-canary20230419061e93e884
7878
- name: Install dependencies
7979
run: npm ci --no-audit
8080
- name: Build
8181
run: npm run build
8282
- name: Test experimental features
8383
env:
84-
ASC_FEATURES: threads,reference-types,gc,exception-handling
84+
ASC_FEATURES: threads,reference-types,gc,exception-handling,stringref
8585
run: |
86-
npm run test:compiler features/threads features/reference-types features/gc features/exception-handling
86+
npm run test:compiler features/threads features/reference-types features/gc features/exception-handling features/stringref
8787
runtimes:
8888
name: "Runtimes"
8989
runs-on: ubuntu-latest

src/builtins.ts

Lines changed: 682 additions & 0 deletions
Large diffs are not rendered by default.

src/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ export namespace CommonNames {
214214
export const Structref = "Structref";
215215
export const Arrayref = "Arrayref";
216216
export const I31ref = "I31ref";
217+
export const Stringref = "Stringref";
218+
export const StringviewWTF8 = "StringviewWTF8";
219+
export const StringviewWTF16 = "StringviewWTF16";
220+
export const StringviewIter = "StringviewIter";
217221
export const String = "String";
218222
export const RegExp = "RegExp";
219223
export const Object = "Object";

src/module.ts

Lines changed: 271 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,9 @@ export const enum StringMeasureOp {
12981298
/** string.is_usv_sequence */
12991299
IsUSV = 3 /* _BinaryenStringMeasureIsUSV */,
13001300
/** stringview_wtf16.length */
1301-
WTF16View = 4 /* _BinaryenStringMeasureWTF16View */
1301+
WTF16View = 4 /* _BinaryenStringMeasureWTF16View */,
1302+
/** string.hash */
1303+
Hash = 5 /* TODO_BinaryenStringMeasureHash */
13021304
}
13031305

13041306
/** Binaryen StringEncode operation constants. */
@@ -1472,14 +1474,6 @@ export class Module {
14721474
return binaryen._BinaryenRefEq(this.ref, left, right);
14731475
}
14741476

1475-
string_eq(left: ExpressionRef, right: ExpressionRef): ExpressionRef {
1476-
return binaryen._BinaryenStringEq(this.ref, StringEqOp.Equal, left, right);
1477-
}
1478-
1479-
string_compare(left: ExpressionRef, right: ExpressionRef): ExpressionRef {
1480-
return binaryen._BinaryenStringEq(this.ref, StringEqOp.Compare, left, right);
1481-
}
1482-
14831477
// expressions
14841478

14851479
unary(
@@ -2136,6 +2130,274 @@ export class Module {
21362130
return binaryen._BinaryenI31Get(this.ref, expr, signed);
21372131
}
21382132

2133+
// stringref
2134+
2135+
string_const(
2136+
str: string
2137+
): ExpressionRef {
2138+
return binaryen._BinaryenStringConst(this.ref, this.allocStringCached(str));
2139+
}
2140+
2141+
string_new_utf8(
2142+
ptr: ExpressionRef,
2143+
length: ExpressionRef,
2144+
memory: string = CommonNames.DefaultMemory // TODO
2145+
): ExpressionRef {
2146+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.UTF8, ptr, length, 0, 0, false);
2147+
}
2148+
2149+
string_new_utf8_array(
2150+
arr: ExpressionRef,
2151+
start: ExpressionRef,
2152+
end: ExpressionRef
2153+
): ExpressionRef {
2154+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.UTF8Array, arr, 0, start, end, false);
2155+
}
2156+
2157+
string_new_lossy_utf8(
2158+
ptr: ExpressionRef,
2159+
length: ExpressionRef,
2160+
memory: string = CommonNames.DefaultMemory // TODO
2161+
): ExpressionRef {
2162+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.Replace, ptr, length, 0, 0, false);
2163+
}
2164+
2165+
string_new_lossy_utf8_array(
2166+
arr: ExpressionRef,
2167+
start: ExpressionRef,
2168+
end: ExpressionRef
2169+
): ExpressionRef {
2170+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.ReplaceArray, arr, 0, start, end, false);
2171+
}
2172+
2173+
string_new_wtf8(
2174+
ptr: ExpressionRef,
2175+
length: ExpressionRef,
2176+
memory: string = CommonNames.DefaultMemory // TODO
2177+
): ExpressionRef {
2178+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.WTF8, ptr, length, 0, 0, false);
2179+
}
2180+
2181+
string_new_wtf8_array(
2182+
arr: ExpressionRef,
2183+
start: ExpressionRef,
2184+
end: ExpressionRef
2185+
): ExpressionRef {
2186+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.WTF8Array, arr, 0, start, end, false);
2187+
}
2188+
2189+
string_new_wtf16(
2190+
ptr: ExpressionRef,
2191+
length: ExpressionRef,
2192+
memory: string = CommonNames.DefaultMemory // TODO
2193+
): ExpressionRef {
2194+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.WTF16, ptr, length, 0, 0, false);
2195+
}
2196+
2197+
string_new_wtf16_array(
2198+
arr: ExpressionRef,
2199+
start: ExpressionRef,
2200+
end: ExpressionRef
2201+
): ExpressionRef {
2202+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.WTF16Array, arr, 0, start, end, false);
2203+
}
2204+
2205+
string_from_code_point(
2206+
codepoint: ExpressionRef
2207+
): ExpressionRef {
2208+
return binaryen._BinaryenStringNew(this.ref, StringNewOp.FromCodePoint, codepoint, 0, 0, 0, false);
2209+
}
2210+
2211+
string_hash(
2212+
str: ExpressionRef
2213+
): ExpressionRef {
2214+
return binaryen._BinaryenStringMeasure(this.ref, StringMeasureOp.Hash, str);
2215+
}
2216+
2217+
string_measure_utf8(
2218+
str: ExpressionRef
2219+
): ExpressionRef {
2220+
return binaryen._BinaryenStringMeasure(this.ref, StringMeasureOp.UTF8, str);
2221+
}
2222+
2223+
string_measure_wtf8(
2224+
str: ExpressionRef
2225+
): ExpressionRef {
2226+
return binaryen._BinaryenStringMeasure(this.ref, StringMeasureOp.WTF8, str);
2227+
}
2228+
2229+
string_measure_wtf16(
2230+
str: ExpressionRef
2231+
): ExpressionRef {
2232+
return binaryen._BinaryenStringMeasure(this.ref, StringMeasureOp.WTF16, str);
2233+
}
2234+
2235+
string_is_usv_sequence(
2236+
str: ExpressionRef
2237+
): ExpressionRef {
2238+
return binaryen._BinaryenStringMeasure(this.ref, StringMeasureOp.IsUSV, str);
2239+
}
2240+
2241+
string_encode_utf8(
2242+
str: ExpressionRef,
2243+
ptr: ExpressionRef,
2244+
memory: string = CommonNames.DefaultMemory // TODO
2245+
): ExpressionRef {
2246+
return binaryen._BinaryenStringEncode(this.ref, StringEncodeOp.UTF8, str, ptr, 0);
2247+
}
2248+
2249+
string_encode_utf8_array(
2250+
str: ExpressionRef,
2251+
arr: ExpressionRef,
2252+
start: ExpressionRef
2253+
): ExpressionRef {
2254+
return binaryen._BinaryenStringEncode(this.ref, StringEncodeOp.UTF8Array, str, arr, start);
2255+
}
2256+
2257+
// TOOD: string_encode_lossy_utf8
2258+
// TODO: string_encode_lossy_utf8_array
2259+
2260+
string_encode_wtf8(
2261+
str: ExpressionRef,
2262+
ptr: ExpressionRef,
2263+
memory: string = CommonNames.DefaultMemory // TODO
2264+
): ExpressionRef {
2265+
return binaryen._BinaryenStringEncode(this.ref, StringEncodeOp.WTF8, str, ptr, 0);
2266+
}
2267+
2268+
string_encode_wtf8_array(
2269+
str: ExpressionRef,
2270+
arr: ExpressionRef,
2271+
start: ExpressionRef
2272+
): ExpressionRef {
2273+
return binaryen._BinaryenStringEncode(this.ref, StringEncodeOp.WTF8Array, str, arr, start);
2274+
}
2275+
2276+
string_encode_wtf16(
2277+
str: ExpressionRef,
2278+
ptr: ExpressionRef,
2279+
memory: string = CommonNames.DefaultMemory // TODO
2280+
): ExpressionRef {
2281+
return binaryen._BinaryenStringEncode(this.ref, StringEncodeOp.WTF16, str, ptr, 0);
2282+
}
2283+
2284+
string_encode_wtf16_array(
2285+
str: ExpressionRef,
2286+
arr: ExpressionRef,
2287+
start: ExpressionRef
2288+
): ExpressionRef {
2289+
return binaryen._BinaryenStringEncode(this.ref, StringEncodeOp.WTF16Array, str, arr, start);
2290+
}
2291+
2292+
string_concat(
2293+
left: ExpressionRef,
2294+
right: ExpressionRef
2295+
): ExpressionRef {
2296+
return binaryen._BinaryenStringConcat(this.ref, left, right);
2297+
}
2298+
2299+
string_eq(
2300+
left: ExpressionRef,
2301+
right: ExpressionRef
2302+
): ExpressionRef {
2303+
return binaryen._BinaryenStringEq(this.ref, StringEqOp.Equal, left, right);
2304+
}
2305+
2306+
string_compare(
2307+
left: ExpressionRef,
2308+
right: ExpressionRef
2309+
): ExpressionRef {
2310+
return binaryen._BinaryenStringEq(this.ref, StringEqOp.Compare, left, right);
2311+
}
2312+
2313+
string_as_wtf8(
2314+
str: ExpressionRef
2315+
): ExpressionRef {
2316+
return binaryen._BinaryenStringAs(this.ref, StringAsOp.WTF8, str);
2317+
}
2318+
2319+
string_as_wtf16(
2320+
str: ExpressionRef
2321+
): ExpressionRef {
2322+
return binaryen._BinaryenStringAs(this.ref, StringAsOp.WTF16, str);
2323+
}
2324+
2325+
string_as_iter(
2326+
str: ExpressionRef
2327+
): ExpressionRef {
2328+
return binaryen._BinaryenStringAs(this.ref, StringAsOp.Iter, str);
2329+
}
2330+
2331+
stringview_wtf8_advance(
2332+
view: ExpressionRef,
2333+
pos: ExpressionRef,
2334+
bytes: ExpressionRef
2335+
): ExpressionRef {
2336+
return binaryen._BinaryenStringWTF8Advance(this.ref, view, pos, bytes);
2337+
}
2338+
2339+
// TODO: stringview_wtf8_encode_utf8
2340+
// TODO: stringview_wtf8_encode_lossy_utf8
2341+
// TODO: stringview_wtf8_encode_wtf8
2342+
2343+
stringview_wtf8_slice(
2344+
view: ExpressionRef,
2345+
start: ExpressionRef,
2346+
end: ExpressionRef
2347+
): ExpressionRef {
2348+
return binaryen._BinaryenStringSliceWTF(this.ref, StringSliceWTFOp.WTF8, view, start, end);
2349+
}
2350+
2351+
stringview_wtf16_length(
2352+
view: ExpressionRef
2353+
): ExpressionRef {
2354+
return binaryen._BinaryenStringMeasure(this.ref, StringMeasureOp.WTF16View, view);
2355+
}
2356+
2357+
stringview_wtf16_slice(
2358+
view: ExpressionRef,
2359+
start: ExpressionRef,
2360+
end: ExpressionRef
2361+
): ExpressionRef {
2362+
return binaryen._BinaryenStringSliceWTF(this.ref, StringSliceWTFOp.WTF16, view, start, end);
2363+
}
2364+
2365+
stringview_wtf16_get_codeunit(
2366+
view: ExpressionRef,
2367+
pos: ExpressionRef
2368+
): ExpressionRef {
2369+
return binaryen._BinaryenStringWTF16Get(this.ref, view, pos);
2370+
}
2371+
2372+
// TODO: stringview_wtf16_encode
2373+
2374+
stringview_iter_next(
2375+
view: ExpressionRef
2376+
): ExpressionRef {
2377+
return binaryen._BinaryenStringIterNext(this.ref, view);
2378+
}
2379+
2380+
stringview_iter_advance(
2381+
view: ExpressionRef,
2382+
count: ExpressionRef
2383+
): ExpressionRef {
2384+
return binaryen._BinaryenStringIterMove(this.ref, StringIterMoveOp.Advance, view, count);
2385+
}
2386+
2387+
stringview_iter_rewind(
2388+
view: ExpressionRef,
2389+
count: ExpressionRef
2390+
): ExpressionRef {
2391+
return binaryen._BinaryenStringIterMove(this.ref, StringIterMoveOp.Rewind, view, count);
2392+
}
2393+
2394+
stringview_iter_slice(
2395+
view: ExpressionRef,
2396+
count: ExpressionRef
2397+
): ExpressionRef {
2398+
return binaryen._BinaryenStringSliceIter(this.ref, view, count);
2399+
}
2400+
21392401
// globals
21402402

21412403
addGlobal(

src/program.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,12 @@ export class Program extends DiagnosticEmitter {
12971297
this.registerWrapperClass(Type.arrayref, CommonNames.Arrayref);
12981298
this.registerWrapperClass(Type.i31ref, CommonNames.I31ref);
12991299
}
1300+
if (options.hasFeature(Feature.Stringref)) {
1301+
this.registerWrapperClass(Type.stringref, CommonNames.Stringref);
1302+
this.registerWrapperClass(Type.stringview_wtf8, CommonNames.StringviewWTF8);
1303+
this.registerWrapperClass(Type.stringview_wtf16, CommonNames.StringviewWTF16);
1304+
this.registerWrapperClass(Type.stringview_iter, CommonNames.StringviewIter);
1305+
}
13001306
}
13011307

13021308
// resolve prototypes of extended classes or interfaces

0 commit comments

Comments
 (0)