From 6be74d4b22cc0adc7561c53815f1114cec2a5531 Mon Sep 17 00:00:00 2001 From: freedomhero Date: Wed, 17 Mar 2021 11:09:41 +0800 Subject: [PATCH] 0.3.2 (#75) * ignore fs & child_process in browser env * use BigInt function over literals to allow third party using bable / uglifyjs etc. Ref: https://github.com/bcoin-org/bcrypto/pull/33 * add timeout option for compile function * bump version to 0.3.2 --- package-lock.json | 2 +- package.json | 6 +++++- src/compilerWrapper.ts | 5 +++-- src/serializer.ts | 8 ++++---- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index f284c967..7a727010 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "scryptlib", - "version": "0.3.1", + "version": "0.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8a29e865..8d5af702 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,16 @@ { "name": "scryptlib", - "version": "0.3.1", + "version": "0.3.2", "description": "Javascript SDK for integration of Bitcoin SV Smart Contracts written in sCrypt language.", "engines": { "node": ">=12.0.0" }, "main": "dist/index.js", "types": "dist", + "browser": { + "child_process": false, + "fs": false + }, "scripts": { "compile": "tsc -b", "watch": "tsc -b -w", diff --git a/src/compilerWrapper.ts b/src/compilerWrapper.ts index 3a7d3c1d..b33203f4 100644 --- a/src/compilerWrapper.ts +++ b/src/compilerWrapper.ts @@ -144,6 +144,7 @@ export function compile( cmdArgs?: string, sourceMap?: boolean, optimize?: boolean, + timeout?: number // in ms } = { asm: true, debug: true, @@ -151,18 +152,18 @@ export function compile( } ): CompileResult { const st = Date.now(); - const npxArg = settings.npxArgs || '--no-install'; const sourcePath = source.path; const srcDir = dirname(sourcePath); const curWorkingDir = settings.cwd || srcDir; const sourceFileName = basename(sourcePath); const outputDir = settings.outputDir || srcDir; + const timeout = settings.timeout || 1200000; const outputFiles = {}; try { const sourceContent = source.content !== undefined ? source.content : readFileSync(sourcePath, 'utf8'); const cmdPrefix = settings.cmdPrefix || getDefaultScryptc(); const cmd = `${cmdPrefix} compile ${settings.asm || settings.desc ? '--asm' : ''} ${settings.ast || settings.desc ? '--ast' : ''} ${settings.debug == false ? '' : '--debug'} ${settings.optimize ? '--opt' : ''} -r -o "${outputDir}" ${settings.cmdArgs ? settings.cmdArgs : ''}`; - let output = execSync(cmd, { input: sourceContent, cwd: curWorkingDir }).toString(); + let output = execSync(cmd, { input: sourceContent, cwd: curWorkingDir, timeout }).toString(); // Because the output of the compiler on the win32 platform uses crlf as a newline, here we change \r\n to \n. make SYNTAX_ERR_REG、SEMANTIC_ERR_REG、IMPORT_ERR_REG work. output = output.split(/\r?\n/g).join('\n'); if (output.startsWith('Error:')) { diff --git a/src/serializer.ts b/src/serializer.ts index d61a18b0..807e1afa 100644 --- a/src/serializer.ts +++ b/src/serializer.ts @@ -101,11 +101,11 @@ class OpState { toBigInt() : bigint { if (this.op.opcodenum === Opcode.OP_1) { - return 1n; + return BigInt(1); } else if (this.op.opcodenum === Opcode.OP_0) { - return 0n; + return BigInt(0); } else if (this.op.opcodenum === Opcode.OP_1NEGATE) { - return -1n; + return BigInt(-1); } else if (this.op.opcodenum >= Opcode.OP_2 && this.op.opcodenum <= Opcode.OP_16) { return BigInt(this.op.opcodenum - Opcode.OP_2 + 2); } else { @@ -115,7 +115,7 @@ class OpState { } toBoolean() : boolean { - return this.toBigInt() !== 0n; + return this.toBigInt() !== BigInt(0); } toHex() : string {