Skip to content

Provide type support to wrapper.ts #615

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 11 additions & 7 deletions bindings/compile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import assert from 'assert';

import {
CompileBindings, CompileJson, CompileJsonCallback, CompileJsonMulti, CompileJsonStandard, CoreBindings, SolJson
} from '../common/types';

import { isNil } from '../common/helpers';
import { bindSolcMethod } from './helpers';

export function setupCompile (solJson, core) {
export function setupCompile (solJson: SolJson, core: CoreBindings): CompileBindings {
return {
compileJson: bindCompileJson(solJson),
compileJsonCallback: bindCompileJsonCallback(solJson, core),
Expand All @@ -22,7 +26,7 @@ export function setupCompile (solJson, core) {
*
* @param solJson The Emscripten compiled Solidity object.
*/
function bindCompileJson (solJson) {
function bindCompileJson (solJson: SolJson): CompileJson {
return bindSolcMethod(
solJson,
'compileJSON',
Expand All @@ -38,7 +42,7 @@ function bindCompileJson (solJson) {
*
* @param solJson The Emscripten compiled Solidity object.
*/
function bindCompileJsonMulti (solJson) {
function bindCompileJsonMulti (solJson: SolJson): CompileJsonMulti {
return bindSolcMethod(
solJson,
'compileJSONMulti',
Expand All @@ -55,7 +59,7 @@ function bindCompileJsonMulti (solJson) {
* @param solJson The Emscripten compiled Solidity object.
* @param coreBindings The core bound Solidity methods.
*/
function bindCompileJsonCallback (solJson, coreBindings) {
function bindCompileJsonCallback (solJson: SolJson, coreBindings: CoreBindings): CompileJsonCallback {
const compileInternal = bindSolcMethod(
solJson,
'compileJSONCallback',
Expand All @@ -79,7 +83,7 @@ function bindCompileJsonCallback (solJson, coreBindings) {
* @param solJson The Emscripten compiled Solidity object.
* @param coreBindings The core bound Solidity methods.
*/
function bindCompileStandard (solJson, coreBindings) {
function bindCompileStandard (solJson: SolJson, coreBindings: CoreBindings): CompileJsonStandard {
let boundFunctionStandard: any = null;
let boundFunctionSolidity: any = null;

Expand Down Expand Up @@ -128,7 +132,7 @@ function bindCompileStandard (solJson, coreBindings) {
}

/**********************
* CALL BACKS
* CALLBACKS
**********************/

function wrapCallback (coreBindings, callback) {
Expand Down Expand Up @@ -162,7 +166,7 @@ function wrapCallbackWithKind (coreBindings, callback) {
}

// calls compile() with args || cb
function runWithCallbacks (solJson, coreBindings, callbacks, compile, args) {
function runWithCallbacks (solJson: SolJson, coreBindings: CoreBindings, callbacks, compile, args) {
if (callbacks) {
assert(typeof callbacks === 'object', 'Invalid callback object specified.');
} else {
Expand Down
21 changes: 11 additions & 10 deletions bindings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { bindSolcMethod, bindSolcMethodWithFallbackFunc } from './helpers';
import translate from '../translate';
import * as semver from 'semver';
import { isNil } from '../common/helpers';
import { Alloc, CoreBindings, License, Reset, SolJson, Version, VersionToSemver } from '../common/types';

export function setupCore (solJson) {
export function setupCore (solJson: SolJson): CoreBindings {
const core = {
alloc: bindAlloc(solJson),
license: bindLicense(solJson),
Expand Down Expand Up @@ -39,7 +40,7 @@ export function setupCore (solJson) {
*
* @param solJson The Emscripten compiled Solidity object.
*/
function bindAlloc (solJson) {
function bindAlloc (solJson: SolJson): Alloc {
const allocBinding = bindSolcMethod(
solJson,
'solidity_alloc',
Expand All @@ -62,7 +63,7 @@ function bindAlloc (solJson) {
*
* @param solJson The Emscripten compiled Solidity object.
*/
function bindVersion (solJson) {
function bindVersion (solJson: SolJson): Version {
return bindSolcMethodWithFallbackFunc(
solJson,
'solidity_version',
Expand All @@ -72,7 +73,7 @@ function bindVersion (solJson) {
);
}

function versionToSemver (version) {
function versionToSemver (version: string): VersionToSemver {
return translate.versionToSemver.bind(this, version);
}

Expand All @@ -83,7 +84,7 @@ function versionToSemver (version) {
*
* @param solJson The Emscripten compiled Solidity object.
*/
function bindLicense (solJson) {
function bindLicense (solJson: SolJson): License {
return bindSolcMethodWithFallbackFunc(
solJson,
'solidity_license',
Expand All @@ -100,7 +101,7 @@ function bindLicense (solJson) {
*
* @param solJson The Emscripten compiled Solidity object.
*/
function bindReset (solJson) {
function bindReset (solJson: SolJson): Reset {
return bindSolcMethod(
solJson,
'solidity_reset',
Expand Down Expand Up @@ -131,7 +132,7 @@ function bindReset (solJson) {
* @param str The source string being copied to a C string.
* @param ptr The pointer location where the C string will be set.
*/
function unboundCopyToCString (solJson, alloc, str, ptr) {
function unboundCopyToCString (solJson: SolJson, alloc, str: string, ptr: number): void {
const length = solJson.lengthBytesUTF8(str);

const buffer = alloc(length + 1);
Expand All @@ -147,15 +148,15 @@ function unboundCopyToCString (solJson, alloc, str, ptr) {
* @param solJson The Emscripten compiled Solidity object.
* @param ptr The pointer location where the C string will be referenced.
*/
function unboundCopyFromCString (solJson, ptr) {
function unboundCopyFromCString (solJson: SolJson, ptr: any): string {
const copyFromCString = solJson.UTF8ToString || solJson.Pointer_stringify;
return copyFromCString(ptr);
}

function unboundAddFunction (solJson, func, signature?) {
function unboundAddFunction (solJson: SolJson, func: (...args: any[]) => any, signature?: string): number {
return (solJson.addFunction || solJson.Runtime.addFunction)(func, signature);
}

function unboundRemoveFunction (solJson, ptr) {
function unboundRemoveFunction (solJson: SolJson, ptr: number) {
return (solJson.removeFunction || solJson.Runtime.removeFunction)(ptr);
}
11 changes: 6 additions & 5 deletions bindings/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { SolJson, SupportedMethods } from '../common/types';
import { isNil } from '../common/helpers';

export function bindSolcMethod (solJson, method, returnType, args, defaultValue) {
export function bindSolcMethod<T> (solJson: SolJson, method: string, returnType: string, args: string[], defaultValue: T): T {
if (isNil(solJson[`_${method}`]) && defaultValue !== undefined) {
return defaultValue;
}

return solJson.cwrap(method, returnType, args);
}

export function bindSolcMethodWithFallbackFunc (solJson, method, returnType, args, fallbackMethod, finalFallback = undefined) {
export function bindSolcMethodWithFallbackFunc<T> (solJson: SolJson, method: string, returnType: string, args: string[], fallbackMethod: string, finalFallback: any = undefined): T {
const methodFunc = bindSolcMethod(solJson, method, returnType, args, null);

if (!isNil(methodFunc)) {
Expand All @@ -18,19 +19,19 @@ export function bindSolcMethodWithFallbackFunc (solJson, method, returnType, arg
return bindSolcMethod(solJson, fallbackMethod, returnType, args, finalFallback);
}

export function getSupportedMethods (solJson) {
export function getSupportedMethods (solJson: SolJson): SupportedMethods {
return {
licenseSupported: anyMethodExists(solJson, 'solidity_license'),
versionSupported: anyMethodExists(solJson, 'solidity_version'),
allocSupported: anyMethodExists(solJson, 'solidity_alloc'),
resetSupported: anyMethodExists(solJson, 'solidity_reset'),
compileJsonSupported: anyMethodExists(solJson, 'compileJSON'),
compileJsonMultiSupported: anyMethodExists(solJson, 'compileJSONMulti'),
compileJsonCallbackSuppported: anyMethodExists(solJson, 'compileJSONCallback'),
compileJsonCallbackSupported: anyMethodExists(solJson, 'compileJSONCallback'),
compileJsonStandardSupported: anyMethodExists(solJson, 'compileStandard', 'solidity_compile')
};
}

function anyMethodExists (solJson, ...names) {
function anyMethodExists (solJson: SolJson, ...names: string[]): boolean {
return names.some(name => !isNil(solJson[`_${name}`]));
}
8 changes: 7 additions & 1 deletion bindings/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { CompileBindings, CoreBindings, SolJson, SupportedMethods } from '../common/types';

import { setupCore } from './core';
import { getSupportedMethods } from './helpers';
import { setupCompile } from './compile';

export default function setupBindings (solJson) {
export default function setupBindings (solJson: SolJson): {
coreBindings: CoreBindings,
compileBindings: CompileBindings,
methodFlags: SupportedMethods,
} {
const coreBindings = setupCore(solJson);
const compileBindings = setupCompile(solJson, coreBindings);
const methodFlags = getSupportedMethods(solJson);
Expand Down
Loading