Skip to content

Commit

Permalink
Merge pull request AssemblyScript#21 from nearprotocol/string-array-i…
Browse files Browse the repository at this point in the history
…nput

Test string array input
  • Loading branch information
vgrichina committed Feb 22, 2019
2 parents 704e67f + 7538db5 commit c90e09a
Show file tree
Hide file tree
Showing 9 changed files with 1,522 additions and 241 deletions.
2 changes: 1 addition & 1 deletion dist/assemblyscript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/assemblyscript.js.map

Large diffs are not rendered by default.

27 changes: 11 additions & 16 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ export class NEARBindingsBuilder extends ExportsWalker {
private generatedDecodeFunctions = new Set<string>();
private exportedClasses: Class[] = [];
private exportedFunctions: Function[] = [];
private filesByImport = new Map<string, string>();

static build(program: Program): string {
return new NEARBindingsBuilder(program).build();
Expand Down Expand Up @@ -406,21 +405,20 @@ export class NEARBindingsBuilder extends ExportsWalker {
}

private tryUsingImport(type: Type, methodName: string): bool {
let importedFile = this.filesByImport.get(type.classReference!.name);
if (importedFile) {
if (this.hasExport(importedFile, methodName)) {
this.sb.push(`import { ${methodName} } from "${importedFile}";`);
return true;
}
let sourcesWithExport = this.program.sources.filter(source =>
this.getExports(source).filter(d => d.name.text == methodName).length > 0);

if (sourcesWithExport.length == 0) {
return false;
}
return false;
}

private hasExport(importedFile: string, name: string): bool {
let importedSource = this.program.sources.filter(
s => "./" + s.normalizedPath == importedFile + ".ts")[0];
if (sourcesWithExport.length > 1) {
console.log(`WARN: more than one file exporting ${methodName}: ${sourcesWithExport.map(s => s.normalizedPath)}`);
}

return this.getExports(importedSource).filter(d => d.name.text == name).length > 0;
let importPath = sourcesWithExport[0].normalizedPath.replace('.ts', '');
this.sb.push(`import { ${methodName} } from "./${importPath}";`);
return true;
}

private generateHandler(type: Type) {
Expand Down Expand Up @@ -589,9 +587,6 @@ export class NEARBindingsBuilder extends ExportsWalker {
.map(declaration => `${declaration.foreignName.text} as ${declaration.name.text}`)
.join(",");
this.sb.push(`import {${declarationsStr}} from "${statement.path.value}";`);
statement.declarations.forEach(d => {
this.filesByImport.set(d.name.text, statement.path.value);
});
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions tests/near-bindgen/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ export function getFoobar(container: ContainerClass): AnotherContainerClass {

export function convertFoobars(foobars: Array<FooBar>): Array<ContainerClass> {
return foobars.map<ContainerClass>((it: FooBar, i: i32, arr: Array<FooBar>): ContainerClass => { let container = new ContainerClass(); container.foobar = it; return container; });
}

export function getStringArrayLength(arr: string[]): i32 {
near.log("getStringArrayLength: " + near.str(arr.length));
return arr.length;
}
61 changes: 60 additions & 1 deletion tests/near-bindgen/main_near.ts.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
doNothing as wrapped_doNothing,
add as wrapped_add,
getFoobar as wrapped_getFoobar,
convertFoobars as wrapped_convertFoobars
convertFoobars as wrapped_convertFoobars,
getStringArrayLength as wrapped_getStringArrayLength
} from "./main";

// Runtime functions
Expand Down Expand Up @@ -289,3 +290,61 @@ export function near_func_convertFoobars(): void {
encoder.popObject();
return_value(near.bufferWithSize(encoder.serialize()).buffer.data);
}
import { __near_decode_Array_String } from "./model_near";
export class __near_ArgsParser_getStringArrayLength extends ThrowingJSONHandler {
buffer: Uint8Array;
decoder: JSONDecoder<__near_ArgsParser_getStringArrayLength>;
handledRoot: boolean = false;

__near_param_arr: Array<String>;
setNull(name: string): void {
if (name == "arr") {
this.__near_param_arr = <Array<String>>null;
return;
}

super.setNull(name);
}

pushObject(name: string): bool {
if (!this.handledRoot) {
assert(name == null);
this.handledRoot = true;
return true;
} else {
assert(name != null);
}

return super.pushObject(name);
}

pushArray(name: string): bool {
if (name == "arr") {
this.__near_param_arr = <Array<String>>(
__near_decode_Array_String(this.buffer, this.decoder.state)
);
return false;
}

return super.pushArray(name);
}
}
export function near_func_getStringArrayLength(): void {
let json = new Uint8Array(input_read_len());
input_read_into(json.buffer.data);
let handler = new __near_ArgsParser_getStringArrayLength();
handler.buffer = json;
handler.decoder = new JSONDecoder<__near_ArgsParser_getStringArrayLength>(
handler
);
handler.decoder.deserialize(json);
let result = wrapped_getStringArrayLength(handler.__near_param_arr);

let encoder = new JSONEncoder();
encoder.pushObject(null);

encoder.setInteger("result", result);

encoder.popObject();
return_value(near.bufferWithSize(encoder.serialize()).buffer.data);
}
9 changes: 8 additions & 1 deletion tests/near-bindgen/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ export namespace near {
let arr: Array<T> = [value];
return arr.toString();
}
}

export function log(msg: string): void {
_near_log(<usize>msg);
}
}

@external("env", "log")
declare function _near_log(msg_ptr: usize): void;
6 changes: 4 additions & 2 deletions tests/near-bindgen/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ async function loadModule(path) {
Object.keys(module).forEach(methodName => {
wrapped[methodName] = async function(inputJson) {
setInputJson(inputJson);
outputJson = null;
await module[methodName].call(module);
const outputJson = getOutputJson();
return outputJson && outputJson.result;
const resultJson = getOutputJson();
return resultJson && resultJson.result;
}
});
return wrapped;
Expand All @@ -81,6 +82,7 @@ async function loadModule(path) {
assert.deepEqual(await module.convertFoobars({
foobars: [{ arr: [["1", "2"], ["3"]] }] }),
[{ foobar: { foo: 0, bar: 1, flag: false, baz: '123', arr: [["1", "2"], ["3"]] }}]);
assert.equal(await module.getStringArrayLength({ arr: ["1", "2", "3"] }), 3);
})().catch(e => {
console.error('Error during test execution:', e);
if (e.code == 'ERR_ASSERTION') {
Expand Down
5 changes: 5 additions & 0 deletions tests/near-bindgen/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import * as main from "./main_near";
import * as model from "./model_near";
import { near } from "./near";

@external("env", "log")
declare function log(str: string): void;
Expand All @@ -20,4 +21,8 @@ export function runTest(): void {

export function convertFoobars(): void {
main.near_func_convertFoobars();
}

export function getStringArrayLength(): void {
main.near_func_getStringArrayLength();
}
Loading

0 comments on commit c90e09a

Please sign in to comment.