walt-compiler
Advanced tools
Comparing version 0.13.0 to 0.15.0
{ | ||
"name": "walt-compiler", | ||
"version": "0.13.0", | ||
"version": "0.15.0", | ||
"description": "Alternative syntax for WebAssembly text format", | ||
@@ -47,4 +47,4 @@ "main": "dist/walt.js", | ||
"nearley": "^2.15.1", | ||
"walt-parser-tools": "^0.2.1", | ||
"walt-syntax": "^0.3.1", | ||
"walt-parser-tools": "^0.2.2", | ||
"walt-syntax": "^0.3.2", | ||
"wasm-types": "1.0.3" | ||
@@ -120,2 +120,3 @@ }, | ||
"src/**/print-node.js", | ||
"src/**/test-utils.js", | ||
"src/**/__tests__/*.js", | ||
@@ -122,0 +123,0 @@ "src/**/grammar.js", |
@@ -27,2 +27,10 @@ import test from 'ava'; | ||
test( | ||
'statics', | ||
harness(path.resolve(__dirname, './statics-spec.walt'), null, { | ||
printBinary: false, | ||
printNode: false, | ||
}) | ||
); | ||
test('import as', t => { | ||
@@ -29,0 +37,0 @@ const parser = makeParser([]); |
@@ -87,2 +87,6 @@ /** | ||
}); | ||
} else { | ||
const bucket = | ||
typeNode.value === 'Memory' ? 'memories' : 'tables'; | ||
context[bucket].push(identifierNode); | ||
} | ||
@@ -89,0 +93,0 @@ |
@@ -10,2 +10,7 @@ /** | ||
const isMemoryIdentifier = (context, id) => { | ||
const memory = context.memories[0]; | ||
return memory && memory.value === id.value; | ||
}; | ||
export default function memoryPlugin(): SemanticPlugin { | ||
@@ -43,6 +48,11 @@ return { | ||
const [decl, context] = args; | ||
const { scopes, memories } = context; | ||
// Short circuit since memory is a special type of declaration | ||
if (!context.locals && decl.type === 'Memory') { | ||
return { | ||
if ( | ||
!scopes.length < 2 && | ||
decl.type === 'Memory' && | ||
!memories.length | ||
) { | ||
memories.push({ | ||
...decl, | ||
@@ -53,3 +63,4 @@ meta: { | ||
}, | ||
}; | ||
}); | ||
return memories[0]; | ||
} | ||
@@ -59,2 +70,54 @@ | ||
}, | ||
[Syntax.FunctionCall]: next => (args, transform) => { | ||
const [node, context] = args; | ||
const [subscript, ...rest] = node.params; | ||
const [id, field = {}] = subscript.params; | ||
const callMap = { | ||
dataSize: { | ||
...id, | ||
type: 'i32', | ||
Type: Syntax.ArraySubscript, | ||
params: [ | ||
{ | ||
...id, | ||
type: 'i32', | ||
value: '0', | ||
Type: Syntax.Constant, | ||
}, | ||
{ | ||
...id, | ||
type: 'i32', | ||
value: '0', | ||
Type: Syntax.Constant, | ||
}, | ||
], | ||
}, | ||
grow: { | ||
...id, | ||
value: 'grow_memory', | ||
params: rest.map(p => transform([p, context])), | ||
Type: Syntax.NativeMethod, | ||
}, | ||
size: { | ||
...id, | ||
value: 'current_memory', | ||
params: [], | ||
Type: Syntax.NativeMethod, | ||
}, | ||
}; | ||
const mapped = callMap[field.value]; | ||
if ( | ||
!( | ||
subscript.Type === Syntax.ArraySubscript && | ||
isMemoryIdentifier(context, id) && | ||
mapped | ||
) | ||
) { | ||
return next(args); | ||
} | ||
return mapped; | ||
}, | ||
}; | ||
@@ -61,0 +124,0 @@ }, |
@@ -14,2 +14,9 @@ /** | ||
const sizeMap = { | ||
i64: 8, | ||
f64: 8, | ||
i32: 4, | ||
f32: 4, | ||
}; | ||
export const getByteOffsetsAndSize = (objectLiteralNode: NodeType) => { | ||
@@ -30,12 +37,3 @@ const offsetsByKey = {}; | ||
offsetsByKey[key] = size; | ||
switch (typeString) { | ||
case 'i64': | ||
case 'f64': | ||
size += 8; | ||
break; | ||
case 'i32': | ||
case 'f32': | ||
default: | ||
size += 4; | ||
} | ||
size += sizeMap[typeString] || 4; | ||
}, | ||
@@ -42,0 +40,0 @@ })(objectLiteralNode); |
@@ -29,3 +29,3 @@ // @flow | ||
// map over all params, if any and encode each on | ||
params.forEach(p => { | ||
params.filter(p => typeof p !== 'undefined').forEach(p => { | ||
let type = varuint32; | ||
@@ -35,3 +35,3 @@ let stringType = 'i32.literal'; | ||
// Memory opcode? | ||
if (kind.code >= 0x28 && kind.code <= 0x3e) { | ||
if (kind.code >= 0x28 && kind.code <= 0x40) { | ||
type = varuint32; | ||
@@ -38,0 +38,0 @@ stringType = 'memory_immediate'; |
@@ -98,2 +98,4 @@ // @flow | ||
scopes: NodeMap[], | ||
memories: NodeType[], | ||
tables: NodeType[], | ||
}; | ||
@@ -100,0 +102,0 @@ export type Transform = ([NodeType, Context]) => NodeType; |
// @flow | ||
import { stringEncoder } from '../utils/string'; | ||
// import { stringEncoder } from '../utils/string'; | ||
import { u32 } from 'wasm-types'; | ||
@@ -14,10 +14,13 @@ import OutputStream from '../utils/output-stream'; | ||
const map: { [string]: number } = {}; | ||
const data = Object.keys(statics).reduce((acc, key) => { | ||
const encoded = stringEncoder(key); | ||
acc.push({ offset: Number(offsetAccumulator), data: encoded }); | ||
map[key] = offsetAccumulator; | ||
offsetAccumulator += encoded.size; | ||
return acc; | ||
}, []); | ||
const data = Object.entries(statics).reduce( | ||
(acc, [key, encoded]: [string, any]) => { | ||
acc.push({ offset: Number(offsetAccumulator), data: encoded }); | ||
map[key] = offsetAccumulator; | ||
offsetAccumulator += encoded.size; | ||
return acc; | ||
}, | ||
[] | ||
); | ||
// reserved stream for the size header | ||
@@ -24,0 +27,0 @@ const lengthStream = new OutputStream(); |
@@ -155,2 +155,10 @@ // @flow | ||
}, | ||
[Syntax.StaticValueList]: node => { | ||
const { value } = node; | ||
return { | ||
...node, | ||
value: String(staticsMap[value]), | ||
Type: Syntax.Constant, | ||
}; | ||
}, | ||
})(ast) | ||
@@ -157,0 +165,0 @@ ); |
@@ -21,2 +21,7 @@ // @flow | ||
const immediates = { | ||
grow_memory: 0, | ||
current_memory: 0, | ||
}; | ||
const generateNative: GeneratorType = (node, parent) => { | ||
@@ -28,3 +33,3 @@ const block = node.params.map(mapSyntax(parent)).reduce(mergeBlock, []); | ||
if (alignCodes[operation] == null) { | ||
block.push({ kind: textMap[node.value], params: [] }); | ||
block.push({ kind: textMap[node.value], params: [immediates[node.value]] }); | ||
} else { | ||
@@ -31,0 +36,0 @@ const alignment = alignCodes[operation]; |
@@ -33,3 +33,3 @@ // @flow | ||
}; | ||
export const VERSION = '0.13.0'; | ||
export const VERSION = '0.15.0'; | ||
@@ -36,0 +36,0 @@ // Used for debugging purposes |
@@ -28,3 +28,3 @@ /** | ||
import memory from '../core/memory'; | ||
import string from '../core/string'; | ||
import statics from '../core/statics'; | ||
import functionPointer from '../core/function-pointer'; | ||
@@ -53,3 +53,3 @@ import struct from '../core/struct'; | ||
memory, | ||
string, | ||
statics, | ||
functionPointer, | ||
@@ -73,3 +73,3 @@ struct, | ||
memory().semantics, | ||
string().semantics, | ||
statics().semantics, | ||
functionPointer().semantics, | ||
@@ -108,2 +108,4 @@ struct().semantics, | ||
scopes: enterScope([], GLOBAL_INDEX), | ||
memories: [], | ||
tables: [], | ||
}; | ||
@@ -110,0 +112,0 @@ // Parse the current ast |
@@ -12,7 +12,7 @@ // @flow | ||
export const TYPE_USER = 'type/user'; | ||
export const TYPE_OBJECT = 'type/object'; | ||
export const TYPE_OBJECT = 'TYPE_OBJECT'; | ||
export const TYPE_INDEX = 'TYPE_INDEX'; | ||
export const OBJECT_SIZE = 'OBJECT_SIZE'; | ||
export const TYPE_CAST = 'type/cast'; | ||
export const OBJECT_KEY_TYPES = 'object/key-types'; | ||
export const OBJECT_KEY_TYPES = 'OBJECT_KEY_TYPES'; | ||
export const CLOSURE_TYPE = 'closure/type'; | ||
@@ -19,0 +19,0 @@ export const AST_METADATA = 'AST_METADATA'; |
@@ -62,3 +62,4 @@ # Snapshot report for `src/utils/__tests__/print-node-spec.js` | ||
`(func simple (result i32) ;␊ | ||
`(memory 1) ;␊ | ||
(func simple (result i32) ;␊ | ||
(local x i32 ; immutable␊ | ||
@@ -65,0 +66,0 @@ (i32.add ;␊ |
@@ -13,2 +13,3 @@ import test from 'ava'; | ||
const node = getAST(` | ||
const memory: Memory = { initial: 1 }; | ||
function simple(): i32 { | ||
@@ -15,0 +16,0 @@ const x: i32 = 1 + 1; |
@@ -11,3 +11,4 @@ import buildTools from 'walt-buildtools'; | ||
import generator from '../generator'; | ||
import { compile, mapNode, walkNode, prettyPrintNode } from '..'; | ||
import { compile, mapNode, walkNode, prettyPrintNode, debug } from '..'; | ||
import print from 'walt-buildtools/print'; | ||
@@ -40,10 +41,14 @@ export const compileAndRun = (src, imports) => | ||
export const harness = (filepath, env) => t => { | ||
export const harness = ( | ||
filepath, | ||
env, | ||
{ printNode = false, printBinary = false } = {} | ||
) => t => { | ||
const memory = new WebAssembly.Memory({ initial: 1 }); | ||
const view = new DataView(memory.buffer); | ||
const decodeText = getText(view); | ||
const parser = makeParser([]); | ||
const fragment = makeFragment(parser); | ||
const { log } = console; | ||
const build = link(filepath, { | ||
@@ -56,6 +61,16 @@ resolve, | ||
semantics(ast) { | ||
return semantics(ast, [], { parser, fragment }); | ||
const sast = semantics(ast, [], { parser, fragment }); | ||
if (printNode) { | ||
log(print(sast)); | ||
} | ||
return sast; | ||
}, | ||
validate, | ||
emitter, | ||
emitter(...args) { | ||
const wasm = emitter(...args); | ||
if (printBinary) { | ||
log(debug(wasm)); | ||
} | ||
return wasm; | ||
}, | ||
generator, | ||
@@ -68,3 +83,3 @@ prettyPrintNode, | ||
MEMORY_OFFSET: 0, | ||
log: console.log, | ||
log, | ||
assert(strPointer, value, expected) { | ||
@@ -71,0 +86,0 @@ const text = decodeText(strPointer); |
@@ -41,18 +41,18 @@ # Snapshot report for `src/validation/__tests__/validation-spec.js` | ||
Error { | ||
offset: 34, | ||
offset: 13, | ||
token: { | ||
col: 19, | ||
line: 4, | ||
col: 22, | ||
line: 2, | ||
lineBreaks: 0, | ||
offset: 67, | ||
text: ';', | ||
offset: 22, | ||
text: '+', | ||
toString: Function tokenToString {}, | ||
type: 'punctuator', | ||
value: ';', | ||
value: '+', | ||
}, | ||
message: `undefined at line 4 col 19:␊ | ||
message: `undefined at line 2 col 22:␊ | ||
␊ | ||
const x: i32;␊ | ||
^␊ | ||
Unexpected punctuator token: ";"␊ | ||
const g: i32 = 2 +␊ | ||
^␊ | ||
Unexpected punctuator token: "+"␊ | ||
`, | ||
@@ -59,0 +59,0 @@ } |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12051
886766
193
Updatedwalt-parser-tools@^0.2.2
Updatedwalt-syntax@^0.3.2