Skip to content

Commit 8180387

Browse files
pkeljeromesimeon
authored andcommitted
High priority runtime operators (#133).
1 parent f9810ce commit 8180387

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

compiler/wasm/wasm_translate.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,13 @@ let rt_op ctx op : Ir.instr list =
179179
in
180180
let open Ir in
181181
match (op : EJsonRuntimeOperators.ejson_runtime_op) with
182+
| EJsonRuntimeEqual -> foreign [i32; i32] [i32]
183+
| EJsonRuntimeCompare -> foreign [i32; i32] [i32]
184+
| EJsonRuntimeRecConcat -> foreign [i32; i32] [i32]
182185
| EJsonRuntimeRecDot -> foreign [i32; i32] [i32]
186+
| EJsonRuntimeEither -> foreign [i32] [i32]
187+
| EJsonRuntimeToLeft -> foreign [i32] [i32]
188+
| EJsonRuntimeToRight -> foreign [i32] [i32]
183189
| _ -> unsupported ("runtime op: " ^ (string_of_runtime_op op))
184190

185191
module Constants = struct

runtimes/assemblyscript/assembly/index.ts

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,60 @@ export function opMathTrunc(a: EjNumber): EjNumber {
268268

269269
// EJson Runtime Operators
270270

271-
// TODO: this seems to be redundant with opStrictEqual, is it?
272271
export function runtimeEqual(a: EjValue, b: EjValue): EjBool {
273-
return opStrictEqual(a, b);
272+
if (a instanceof EjNull && b instanceof EjNull) {
273+
return c_true;
274+
}
275+
if (a instanceof EjBool && b instanceof EjBool) {
276+
let aa : EjBool = changetype<EjBool>(a) ;
277+
let bb : EjBool = changetype<EjBool>(b) ;
278+
return aa.value == bb.value ? c_true : c_false;
279+
}
280+
if (a instanceof EjNumber && b instanceof EjNumber) {
281+
let aa : EjNumber = changetype<EjNumber>(a) ;
282+
let bb : EjNumber = changetype<EjNumber>(b) ;
283+
return aa.value == bb.value ? c_true : c_false;
284+
}
285+
if (a instanceof EjBigInt && b instanceof EjBigInt) {
286+
let aa : EjBigInt = changetype<EjBigInt>(a) ;
287+
let bb : EjBigInt = changetype<EjBigInt>(b) ;
288+
return aa.value == bb.value ? c_true : c_false;
289+
}
290+
if (a instanceof EjString && b instanceof EjString) {
291+
let aa : EjString = changetype<EjString>(a) ;
292+
let bb : EjString = changetype<EjString>(b) ;
293+
return aa.value == bb.value ? c_true : c_false;
294+
}
295+
if (a instanceof EjArray && b instanceof EjArray) {
296+
let aa : EjArray = changetype<EjArray>(a) ;
297+
let bb : EjArray = changetype<EjArray>(b) ;
298+
if (aa.values.length != bb.values.length) {
299+
return c_false;
300+
}
301+
for (let i = 0; i < aa.values.length; i++) {
302+
if (! runtimeEqual(aa.values[i], bb.values[i]).value) {
303+
return c_false;
304+
}
305+
}
306+
return c_true;
307+
}
308+
if (a instanceof EjObject && b instanceof EjObject) {
309+
let aa : EjObject = changetype<EjObject>(a) ;
310+
let bb : EjObject = changetype<EjObject>(b) ;
311+
if (aa.values.size != bb.values.size) {
312+
return c_false;
313+
}
314+
let keys = aa.values.keys();
315+
for (let i = 0; i < keys.length; i++) {
316+
let k = keys[i];
317+
if (! bb.values.has(k) ||
318+
! runtimeEqual(aa.values[k], bb.values[k]).value ) {
319+
return c_false;
320+
}
321+
}
322+
return c_true;
323+
}
324+
return unreachable();
274325
}
275326

276327
function compare<T>(a: T, b: T): EjNumber {
@@ -293,7 +344,38 @@ export function runtimeCompare(a: EjValue, b: EjValue): EjNumber {
293344
return unreachable();
294345
}
295346

296-
// TODO: this seems to be redundant with opAccess, is it?
297-
export function runtimeRecDot(a: EjObject, b:EjString): EjValue {
298-
return opAccess(a, b);
347+
// TODO: recConcat which argument "wins" in case of a conflict?
348+
export function runtimeRecConcat(a: EjObject, b:EjObject): EjObject {
349+
let r = new EjObject();
350+
let k = a.values.keys();
351+
for (let i = 0; i < k.length; i++) {
352+
r.values.set(k[i], a.values.get(k[i]));
353+
}
354+
k = b.values.keys();
355+
for (let i = 0; i < k.length; i++) {
356+
r.values.set(k[i], b.values.get(k[i]));
357+
}
358+
return r;
359+
}
360+
361+
export function runtimeRecDot(a: EjObject, k:EjString): EjValue {
362+
// TODO: runtimeRecDot redundant with opAccess?
363+
// TODO: runtimeRecDot: check for key not found needed?
364+
return a.values.get(k.value);
365+
}
366+
367+
export function runtimeEither(a: EjValue): EjBool {
368+
if (a instanceof EjLeft || a instanceof EjRight) {
369+
return c_true;
370+
} else {
371+
return c_false;
372+
}
373+
}
374+
375+
export function runtimeToLeft(a: EjLeft): EjValue {
376+
return a.value;
377+
}
378+
379+
export function runtimeToRight(a: EjRight): EjValue {
380+
return a.value;
299381
}

0 commit comments

Comments
 (0)