Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.1.15 #14

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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