Skip to content

Commit

Permalink
Clean up encoding of memories
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Jun 6, 2020
1 parent 5b7219e commit b3900b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
14 changes: 10 additions & 4 deletions packages/compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
TYPE_IDX,
EXPORT_TYPE,
SECTION,
MEMORY_IDX,
MAGIC,
WASM_VERSION,
} from "./encoding";
import shims from "./shims";
import { localFuncMap } from "./wasmFunctions";
Expand Down Expand Up @@ -184,7 +187,11 @@ export function compileModule({

const memories = [
// Only one memory
[...unsignedLEB128(0), ...unsignedLEB128(WASM_MEMORY_SIZE)],
[
0x01, // Indicates that we are specifying two values (initial/max)
...unsignedLEB128(WASM_MEMORY_SIZE), // Initial size
...unsignedLEB128(WASM_MEMORY_SIZE), // Max size
],
];

// https://webassembly.github.io/spec/core/binary/modules.html#global-section
Expand Down Expand Up @@ -219,12 +226,11 @@ export function compileModule({
// the binary functions can be skipped efficiently.
return encodeVector([...encodeVector(localTypes), ...func.binary, op.end]);
});

return new Uint8Array([
// Magic module header
...[0x00, 0x61, 0x73, 0x6d],
...MAGIC,
// Version number
...[0x01, 0x00, 0x00, 0x00],
...WASM_VERSION,
...encodeSection(SECTION.TYPE, types),
...encodeSection(SECTION.IMPORT, imports),
...encodeSection(SECTION.FUNC, functions),
Expand Down
4 changes: 4 additions & 0 deletions packages/compiler/src/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as ieee754 from "./ieee754";
import { flattenTwice } from "./arrayUtils";

export const MAGIC = [0x00, 0x61, 0x73, 0x6d];
export const WASM_VERSION = [0x01, 0x00, 0x00, 0x00];

export const EPSILON = 0.00001;

// An intial attempt to construct a Wasm binary by hand.
Expand Down Expand Up @@ -189,6 +192,7 @@ export type BlockType = typeof BLOCK[BlockKeys];
// http://webassembly.github.io/spec/core/binary/types.html#function-types
export const FUNCTION_TYPE = 0x60;
// I think these might actually be specific to importdesc
export const MEMORY_IDX = 0x02;
export const GLOBAL_TYPE = 0x03;
export const TYPE_IDX = 0x00;

Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/tools/testCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ const testCases: [string, string, number][] = [
["Greater than or equal (false)", "g = 1 >= 2;", 0],
["Script without trailing semi", "g = 1", 1],
["Megabuf access", "g = megabuf(1);", 0],
// ["Max index megabuf", "megabuf(8388608) = 10; g = megabuf(8388608);", 10],
// ["Max index + 1 megabuf", "megabuf(8388609) = 10; g = megabuf(8388609);", 0],
["Max index megabuf", "megabuf(8388608) = 10; g = megabuf(8388608);", 10],
["Max index + 1 megabuf", "megabuf(8388609) = 10; g = megabuf(8388609);", 0],
// ["Max index gmegabuf", " g = gmegabuf(8388608);", 10],
// ["Max index+1 gmegabuf", "gmegabuf(8388609) = 10; g = gmegabuf(8388609);", 0],
["Max index+1 gmegabuf", "gmegabuf(8388609) = 10; g = gmegabuf(8388609);", 0],
["Megabuf assignment", "megabuf(1) = 10; g = megabuf(1);", 10],
["Megabuf assignment (idx 100)", "megabuf(100) = 10; g = megabuf(100);", 10],
["Megabuf (float)", "megabuf(0) = 1.2; g = megabuf(0);", 1.2],
Expand Down

0 comments on commit b3900b7

Please sign in to comment.