Skip to content

Commit

Permalink
Merge pull request #14 from wavesplatform/v1.1.15
Browse files Browse the repository at this point in the history
V1.1.15
  • Loading branch information
nazhmik committed Jul 10, 2020
2 parents ab96df6 + c48fccd commit 8029366
Show file tree
Hide file tree
Showing 5 changed files with 3,068 additions and 3,669 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@waves/ride-js",
"version": "1.1.13",
"version": "1.1.15",
"description": "Js compiler for Ride - Waves smart contract language.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
20 changes: 19 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export interface ICompilationResult {
bytes: Uint8Array
size: number
complexity: number
verifierComplexity?: number
callableComplexity?: Record<string, number>
userFunctionsComplexity?: Record<string, number>
}
}

Expand Down Expand Up @@ -64,7 +67,21 @@ export interface IScriptInfo {
imports: string[]
}

export function compile(code: string, libraries?: { [key: string]: string }): ICompilationResult | ICompilationError;
export interface IFlattenedCompilationResult {
ast?: object
base64?: string
bytes?: Uint8Array
size?: number
complexity?: number
verifierComplexity?: number
callableComplexities?: Record<string, number>
userFunctionComplexities?: Record<string, number>
error?: string
}

export function compile(code: string, estimatorVersion?: number): ICompilationResult | ICompilationError;

export function flattenCompilationResult(compiled: ICompilationResult | ICompilationError): IFlattenedCompilationResult

export function scriptInfo(code: string): IScriptInfo | ICompilationError;

Expand Down Expand Up @@ -97,6 +114,7 @@ export const contractLimits: {
MaxExprSizeInBytes: number,
MaxContractSizeInBytes: number,
MaxContractInvocationArgs: number,
MaxAccountVerifierComplexityByVersion: number
MaxContractInvocationSizeInBytes: number,
MaxWriteSetSizeInBytes: number,
MaxPaymentAmount: number
Expand Down
33 changes: 29 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@ require('./interop');
const crypto = require('@waves/ts-lib-crypto');
const scalaJsCompiler = require('./lang-opt.js');

function wrappedCompile(code, libraries) {
function wrappedCompile(code, estimatorVersion = 2) {
if (typeof code !== 'string') {
return {
error: 'Type error: contract should be string'
}
}
try {
const result = scalaJsCompiler.compile(code, libraries);
const result = scalaJsCompiler.compile(code, estimatorVersion);
if (result.error) {
try {
result.size = new Uint8Array(result.result).length;
} catch (e) {
}
return result;
} else {
const bytes = new Uint8Array(result.result);
const {ast, complexity, verifierComplexity, callableComplexities, userFunctionComplexities} = result;
return {
result: {
bytes,
base64: crypto.base64Encode(bytes),
size: bytes.byteLength,
ast: result.ast,
complexity: result.complexity,
ast,
complexity,
verifierComplexity,
callableComplexities,
userFunctionComplexities,
}
}
}
} catch (e) {
console.log(e)
return typeof e === 'object' ?
{error: e.message} :
{error: e}
Expand All @@ -51,6 +60,21 @@ function wrappedRepl(opts) {
return repl
}

const flattenCompilationResult = (compiled) => {
let result = {};
if (compiled.error) {
if (compiled.result) {
const bytes = new Uint8Array(compiled.result);
const base64 = crypto.base64Encode(bytes);
result = {...compiled, base64};
result.result && delete result.result
}
} else {
result = compiled.result
}
return result
}

const api = {
compile: wrappedCompile,
repl: wrappedRepl,
Expand All @@ -66,6 +90,7 @@ const api = {
getVarsDoc: scalaJsCompiler.getVarsDoc,
getFunctionsDoc: scalaJsCompiler.getFunctionsDoc,
decompile: scalaJsCompiler.decompile,
flattenCompilationResult
}

global.RideJS = api;
Expand Down
Loading

0 comments on commit 8029366

Please sign in to comment.