From 740340a076dc582af915b50f1e16d0ac436d6843 Mon Sep 17 00:00:00 2001 From: Dmitry Iv Date: Mon, 12 Aug 2024 14:26:59 -0400 Subject: [PATCH] Fix data offset case --- src/compile.js | 12 ++++++------ test/compile.js | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/compile.js b/src/compile.js index c89a0d0..7b20892 100644 --- a/src/compile.js +++ b/src/compile.js @@ -342,7 +342,7 @@ const build = { // (data (i32.const 0) "\aa" "\bb"?) data([, offset, ...inits], ctx) { - // FIXME: first is mem index + if (offset[0] === 'offset') [, offset] = offset ctx.data.push([0, ...iinit(offset, ctx), ...str(inits.map(i => i[0] === '"' ? i.slice(1, -1) : i).join(''))]) }, @@ -354,11 +354,11 @@ const build = { // (i32.const 0) - instantiation time initializer const iinit = ([op, literal], ctx) => - op.startsWith('f32') ? [OP_F32_CONST, ...f32(literal), OP_END] : - op.startsWith('f64') ? [OP_F64_CONST, ...f64(literal), OP_END] : - op.startsWith('i32') ? [OP_I32_CONST, ...leb(literal[0] === '$' ? ctx.global[literal] : literal), OP_END] : - op.startsWith('i64') ? [OP_I64_CONST, ...bigleb(literal[0] === '$' ? ctx.global[literal] : literal), OP_END] : - err('Unknown init') + op === 'f32.const' ? [OP_F32_CONST, ...f32(literal), OP_END] : + op === 'f64.const' ? [OP_F64_CONST, ...f64(literal), OP_END] : + op === 'i32.const' ? [OP_I32_CONST, ...leb(literal[0] === '$' ? ctx.global[literal] : literal), OP_END] : + op === 'i64.const' ? [OP_I64_CONST, ...bigleb(literal[0] === '$' ? ctx.global[literal] : literal), OP_END] : + err(`Unknown init ${op} ${literal}`) const escape = { n: 10, r: 13, t: 9, v: 1, '\\': 92 } diff --git a/test/compile.js b/test/compile.js index 64e5771..343bbee 100644 --- a/test/compile.js +++ b/test/compile.js @@ -1151,7 +1151,7 @@ t('case: data content', () => { is(compile(parse(src)), wat2wasm(src).buffer) }) -t.todo('case: data offset', () => { +t('case: data offset', () => { let src = `(data (offset (i32.const 65505)) "\\16\\17\\18\\19\\20\\21\\22\\23\\24\\25\\26\\27\\28\\29\\30\\31")` is(compile(parse(src)), wat2wasm(src).buffer) })