#!/usr/bin/env python3 # # Copyright 2016 WebAssembly Community Group participants # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Convert a JSON descrption of a spec test into a JavaScript.""" import argparse import io import json import os import re import struct import sys import find_exe from utils import ChangeDir, ChangeExt, Error, Executable import utils SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) F32_INF = 0x7f800000 F32_NEG_INF = 0xff800000 F32_NEG_ZERO = 0x80000000 F32_SIGN_BIT = F32_NEG_ZERO F32_SIG_MASK = 0x7fffff F32_QUIET_NAN = 0x7fc00000 F32_QUIET_NAN_TAG = 0x400000 F64_INF = 0x7ff0000000000000 F64_NEG_INF = 0xfff0000000000000 F64_NEG_ZERO = 0x8000000000000000 F64_SIGN_BIT = F64_NEG_ZERO F64_SIG_MASK = 0xfffffffffffff F64_QUIET_NAN = 0x7ff8000000000000 F64_QUIET_NAN_TAG = 0x8000000000000 def I32ToJS(value): # JavaScript will return all i32 values as signed. if value >= 2**31: value -= 2**32 return str(value) def IsNaNF32(f32_bits): return (F32_INF < f32_bits < F32_NEG_ZERO) or (f32_bits > F32_NEG_INF) def ReinterpretF32(f32_bits): return struct.unpack(' F64_NEG_INF) def ReinterpretF64(f64_bits): return struct.unpack(' %s, %s);\n' % (self._Action(command['action']), self._ConstantList(expected))) elif len(expected) == 0: self._WriteAssertActionCommand(command) else: raise Error('Unexpected result with multiple values: %s' % expected) def _WriteAssertActionCommand(self, command): self.out_file.write('%s(() => %s);\n' % (command['type'], self._Action(command['action']))) def _Module(self, filename): with open(os.path.join(self.base_dir, filename), 'rb') as wasm_file: return ''.join('\\x%02x' % c for c in bytearray(wasm_file.read())) def _Constant(self, const): assert IsValidJSConstant(const), 'Invalid JS const: %s' % const type_ = const['type'] value = const['value'] if type_ in ('f32', 'f64') and value in ('nan:canonical', 'nan:arithmetic'): return value if type_ == 'i32': return I32ToJS(int(value)) elif type_ == 'f32': return F32ToJS(int(value)) elif type_ == 'f64': return F64ToJS(int(value)) else: assert False def _ConstantList(self, consts): return ', '.join(self._Constant(const) for const in consts) def _Action(self, action): type_ = action['type'] module = action.get('module', self._ModuleIdxName()) field = EscapeJSString(action['field']) if type_ == 'invoke': args = '[%s]' % self._ConstantList(action.get('args', [])) return 'call(%s, "%s", %s)' % (module, field, args) elif type_ == 'get': return 'get(%s, "%s")' % (module, field) else: raise Error('Unexpected action type: %s' % type_) def main(args): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-o', '--output', metavar='PATH', help='output file.') parser.add_argument('-P', '--prefix', metavar='PATH', help='prefix file.', default=os.path.join(SCRIPT_DIR, 'gen-spec-prefix.js')) parser.add_argument('--bindir', metavar='PATH', default=find_exe.GetDefaultPath(), help='directory to search for all executables.') parser.add_argument('--temp-dir', metavar='PATH', help='set the directory that temporary wasm/wat' ' files are written.') parser.add_argument('--no-error-cmdline', help='don\'t display the subprocess\'s commandline when' ' an error occurs', dest='error_cmdline', action='store_false') parser.add_argument('-p', '--print-cmd', help='print the commands that are run.', action='store_true') parser.add_argument('file', help='spec json file.') options = parser.parse_args(args) wat2wasm = Executable( find_exe.GetWat2WasmExecutable(options.bindir), error_cmdline=options.error_cmdline) wasm2wat = Executable( find_exe.GetWasm2WatExecutable(options.bindir), error_cmdline=options.error_cmdline) wat2wasm.verbose = options.print_cmd wasm2wat.verbose = options.print_cmd with open(options.file) as json_file: json_dir = os.path.dirname(options.file) spec_json = json.load(json_file) all_commands = spec_json['commands'] # modules is a list of pairs: [(module_command, [assert_command, ...]), ...] modules = CollectInvalidModuleCommands(all_commands) with utils.TempDirectory(options.temp_dir, 'gen-spec-js-') as temp_dir: extender = ModuleExtender(wat2wasm, wasm2wat, temp_dir) for module_command, assert_commands in modules: if assert_commands: wasm_path = os.path.join(json_dir, module_command['filename']) new_module_filename = extender.Extend(wasm_path, assert_commands) module_command['filename'] = new_module_filename output = io.StringIO() if options.prefix: with open(options.prefix) as prefix_file: output.write(prefix_file.read()) output.write('\n') JSWriter(json_dir, spec_json, output).Write() if options.output: out_file = open(options.output, 'w') else: out_file = sys.stdout try: out_file.write(output.getvalue()) finally: out_file.close() return 0 if __name__ == '__main__': try: sys.exit(main(sys.argv[1:])) except Error as e: sys.stderr.write(str(e) + '\n') sys.exit(1)