Skip to content

Commit 9d42abb

Browse files
committed
Put generated and wrapped file in separate modules
Required some hackery cause cannot import module as a whole, see: AssemblyScript#423
1 parent 906b8fd commit 9d42abb

File tree

5 files changed

+60
-34
lines changed

5 files changed

+60
-34
lines changed

dist/asc.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

dist/asc.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist/assemblyscript.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

dist/assemblyscript.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/definitions.ts

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ export class NEARBindingsBuilder extends ExportsWalker {
140140
private sb: string[] = [];
141141
private generatedEncodeFunctions = new Set<string>();
142142
private generatedDecodeFunctions = new Set<string>();
143+
private exportedClasses: Class[] = [];
144+
private exportedFunctions: Function[] = [];
143145

144146
static build(program: Program): string {
145147
return new NEARBindingsBuilder(program).build();
@@ -154,14 +156,33 @@ export class NEARBindingsBuilder extends ExportsWalker {
154156
}
155157

156158
visitClass(element: Class): void {
157-
// Do nothing
159+
if (!element.is(CommonFlags.EXPORT)) {
160+
return;
161+
}
162+
this.exportedClasses.push(element);
158163
}
159164

160165
visitFunction(element: Function): void {
166+
if (!element.is(CommonFlags.EXPORT)) {
167+
return;
168+
}
169+
this.exportedFunctions.push(element);
161170
this.generateArgsParser(element);
162171
this.generateWrapperFunction(element);
163172
}
164173

174+
visitInterface(element: Interface): void {
175+
// Do nothing
176+
}
177+
178+
visitField(element: Field): void {
179+
throw new Error("Shouldn't be called");
180+
}
181+
182+
visitNamespace(element: Element): void {
183+
// Do nothing
184+
}
185+
165186
private generateArgsParser(element: Function) {
166187
let signature = element.signature;
167188
let fields = signature.parameterNames ? signature.parameterNames.map((paramName, i) => {
@@ -175,7 +196,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
175196
`);
176197
if (signature.parameterNames) {
177198
fields.forEach((field) => {
178-
this.sb.push(`__near_param_${field.simpleName}: ${field.type};`);
199+
this.sb.push(`__near_param_${field.simpleName}: ${this.wrappedTypeName(field.type)};`);
179200
});
180201
this.generateHandlerMethods("this.__near_param_", fields);
181202
} else {
@@ -196,9 +217,9 @@ export class NEARBindingsBuilder extends ExportsWalker {
196217
handler.decoder = new JSONDecoder<__near_ArgsParser_${element.simpleName}>(handler);
197218
handler.decoder.deserialize(json);`);
198219
if (returnType.toString() != "void") {
199-
this.sb.push(`let result = ${element.simpleName}(`);
220+
this.sb.push(`let result = wrapped_${element.simpleName}(`);
200221
} else {
201-
this.sb.push(`${element.simpleName}(`);
222+
this.sb.push(`wrapped_${element.simpleName}(`);
202223
}
203224
if (signature.parameterNames) {
204225
this.sb.push(signature.parameterNames.map(paramName => `handler.__near_param_${paramName}`).join(","));
@@ -238,7 +259,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
238259
this.sb.push("setNull(name: string): void {");
239260
fields.forEach((field) => {
240261
this.sb.push(`if (name == "${field.simpleName}") {
241-
${valuePrefix}${field.simpleName} = <${field.type.toString()}>null;
262+
${valuePrefix}${field.simpleName} = <${this.wrappedTypeName(field.type)}>null;
242263
return;
243264
}`);
244265
});
@@ -326,7 +347,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
326347
this.generateEncodeFunction(type.classReference.typeArguments![0]);
327348

328349
this.sb.push(`export function __near_encode_${typeName}(
329-
value: ${type.toString()},
350+
value: ${this.wrappedTypeName(type)},
330351
encoder: JSONEncoder): void {`);
331352
this.sb.push(`for (let i = 0; i < value.length; i++) {`);
332353
this.generateFieldEncoder(type.classReference.typeArguments![0], "null", "value[i]");
@@ -339,7 +360,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
339360
});
340361

341362
this.sb.push(`export function __near_encode_${typeName}(
342-
value: ${type.toString()},
363+
value: ${this.wrappedTypeName(type)},
343364
encoder: JSONEncoder): void {`);
344365
this.getFields(type.classReference).forEach((field) => {
345366
let fieldType = field.type;
@@ -358,7 +379,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
358379
buffer: Uint8Array;
359380
decoder: JSONDecoder<__near_JSONHandler_${typeName}>;
360381
handledRoot: boolean = false;
361-
value: ${type} = new ${type}();`);
382+
value: ${this.wrappedTypeName(type)} = new ${this.wrappedTypeName(type)}();`);
362383
if (this.isArrayType(type)) {
363384
this.generateArrayHandlerMethods("this.value", type.classReference!.typeArguments![0]);
364385
} else {
@@ -367,6 +388,25 @@ export class NEARBindingsBuilder extends ExportsWalker {
367388
this.sb.push("}\n");
368389
}
369390

391+
private wrappedTypeName(type: Type): string {
392+
if (!type.classReference) {
393+
return type.toString();
394+
}
395+
return this.wrappedClassName(type.classReference);
396+
}
397+
398+
private wrappedClassName(cls: Class): string {
399+
if (this.exportedClasses.indexOf(cls) != -1) {
400+
return "wrapped_" + cls.simpleName;
401+
}
402+
if (cls.typeArguments && cls.typeArguments.length > 0) {
403+
return cls.prototype.simpleName + "<" +
404+
cls.typeArguments.map(argType => this.wrappedTypeName(argType)).join(", ") +
405+
">"
406+
}
407+
return cls.simpleName;
408+
}
409+
370410
private generateDecodeFunction(type: Type) {
371411
if (!type.classReference) {
372412
return;
@@ -390,7 +430,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
390430
}
391431

392432
this.sb.push(`export function __near_decode_${typeName}(
393-
buffer: Uint8Array, state: DecoderState): ${type} {
433+
buffer: Uint8Array, state: DecoderState):${this.wrappedTypeName(type)} {
394434
let handler = new __near_JSONHandler_${typeName}();
395435
handler.buffer = buffer;
396436
handler.decoder = new JSONDecoder<__near_JSONHandler_${typeName}>(handler);
@@ -444,23 +484,17 @@ export class NEARBindingsBuilder extends ExportsWalker {
444484
return <Field[]>[...element.members.values()].filter(member => member instanceof Field);
445485
}
446486

447-
visitInterface(element: Interface): void {
448-
// Do nothing
449-
}
450-
451-
visitField(element: Field): void {
452-
throw new Error("Shouldn't be called");
453-
}
454-
455-
visitNamespace(element: Element): void {
456-
// Do nothing
457-
}
458-
459487
build(): string {
460-
this.sb.push(`
488+
this.walk();
489+
let allExported = (<Element[]>this.exportedClasses).concat(<Element[]>this.exportedFunctions);
490+
let allImportsStr = allExported.map(c => `${c.simpleName} as wrapped_${c.simpleName}`).join(", ");
491+
let mainSource = this.program.sources
492+
.filter(s => s.normalizedPath.indexOf("~lib") != 0)[0];
493+
this.sb = [`
461494
import { near } from "./near";
462495
import { JSONEncoder} from "./json/encoder"
463496
import { JSONDecoder, ThrowingJSONHandler, DecoderState } from "./json/decoder"
497+
import {${allImportsStr}} from "./${mainSource.normalizedPath.replace(".ts", "")}";
464498
465499
// Runtime functions
466500
@external("env", "return_value")
@@ -469,11 +503,10 @@ export class NEARBindingsBuilder extends ExportsWalker {
469503
declare function input_read_len(): u32;
470504
@external("env", "input_read_into")
471505
declare function input_read_into(ptr: usize): void;
472-
`);
473-
let mainSource = this.program.sources
474-
.filter(s => s.normalizedPath.indexOf("~lib") != 0)[0];
475-
this.sb.push(mainSource.text);
476-
this.walk();
506+
`].concat(this.sb);
507+
this.exportedClasses.forEach(c => {
508+
this.sb.push(`export class ${c.simpleName} extends ${this.wrappedClassName(c)} {}`);
509+
})
477510
return this.sb.join("\n");
478511
}
479512
}

0 commit comments

Comments
 (0)