@@ -140,6 +140,8 @@ export class NEARBindingsBuilder extends ExportsWalker {
140
140
private sb : string [ ] = [ ] ;
141
141
private generatedEncodeFunctions = new Set < string > ( ) ;
142
142
private generatedDecodeFunctions = new Set < string > ( ) ;
143
+ private exportedClasses : Class [ ] = [ ] ;
144
+ private exportedFunctions : Function [ ] = [ ] ;
143
145
144
146
static build ( program : Program ) : string {
145
147
return new NEARBindingsBuilder ( program ) . build ( ) ;
@@ -154,14 +156,33 @@ export class NEARBindingsBuilder extends ExportsWalker {
154
156
}
155
157
156
158
visitClass ( element : Class ) : void {
157
- // Do nothing
159
+ if ( ! element . is ( CommonFlags . EXPORT ) ) {
160
+ return ;
161
+ }
162
+ this . exportedClasses . push ( element ) ;
158
163
}
159
164
160
165
visitFunction ( element : Function ) : void {
166
+ if ( ! element . is ( CommonFlags . EXPORT ) ) {
167
+ return ;
168
+ }
169
+ this . exportedFunctions . push ( element ) ;
161
170
this . generateArgsParser ( element ) ;
162
171
this . generateWrapperFunction ( element ) ;
163
172
}
164
173
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
+
165
186
private generateArgsParser ( element : Function ) {
166
187
let signature = element . signature ;
167
188
let fields = signature . parameterNames ? signature . parameterNames . map ( ( paramName , i ) => {
@@ -175,7 +196,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
175
196
` ) ;
176
197
if ( signature . parameterNames ) {
177
198
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 ) } ;` ) ;
179
200
} ) ;
180
201
this . generateHandlerMethods ( "this.__near_param_" , fields ) ;
181
202
} else {
@@ -196,9 +217,9 @@ export class NEARBindingsBuilder extends ExportsWalker {
196
217
handler.decoder = new JSONDecoder<__near_ArgsParser_${ element . simpleName } >(handler);
197
218
handler.decoder.deserialize(json);` ) ;
198
219
if ( returnType . toString ( ) != "void" ) {
199
- this . sb . push ( `let result = ${ element . simpleName } (` ) ;
220
+ this . sb . push ( `let result = wrapped_ ${ element . simpleName } (` ) ;
200
221
} else {
201
- this . sb . push ( `${ element . simpleName } (` ) ;
222
+ this . sb . push ( `wrapped_ ${ element . simpleName } (` ) ;
202
223
}
203
224
if ( signature . parameterNames ) {
204
225
this . sb . push ( signature . parameterNames . map ( paramName => `handler.__near_param_${ paramName } ` ) . join ( "," ) ) ;
@@ -238,7 +259,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
238
259
this . sb . push ( "setNull(name: string): void {" ) ;
239
260
fields . forEach ( ( field ) => {
240
261
this . sb . push ( `if (name == "${ field . simpleName } ") {
241
- ${ valuePrefix } ${ field . simpleName } = <${ field . type . toString ( ) } >null;
262
+ ${ valuePrefix } ${ field . simpleName } = <${ this . wrappedTypeName ( field . type ) } >null;
242
263
return;
243
264
}` ) ;
244
265
} ) ;
@@ -326,7 +347,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
326
347
this . generateEncodeFunction ( type . classReference . typeArguments ! [ 0 ] ) ;
327
348
328
349
this . sb . push ( `export function __near_encode_${ typeName } (
329
- value: ${ type . toString ( ) } ,
350
+ value: ${ this . wrappedTypeName ( type ) } ,
330
351
encoder: JSONEncoder): void {` ) ;
331
352
this . sb . push ( `for (let i = 0; i < value.length; i++) {` ) ;
332
353
this . generateFieldEncoder ( type . classReference . typeArguments ! [ 0 ] , "null" , "value[i]" ) ;
@@ -339,7 +360,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
339
360
} ) ;
340
361
341
362
this . sb . push ( `export function __near_encode_${ typeName } (
342
- value: ${ type . toString ( ) } ,
363
+ value: ${ this . wrappedTypeName ( type ) } ,
343
364
encoder: JSONEncoder): void {` ) ;
344
365
this . getFields ( type . classReference ) . forEach ( ( field ) => {
345
366
let fieldType = field . type ;
@@ -358,7 +379,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
358
379
buffer: Uint8Array;
359
380
decoder: JSONDecoder<__near_JSONHandler_${ typeName } >;
360
381
handledRoot: boolean = false;
361
- value: ${ type } = new ${ type } ();` ) ;
382
+ value: ${ this . wrappedTypeName ( type ) } = new ${ this . wrappedTypeName ( type ) } ();` ) ;
362
383
if ( this . isArrayType ( type ) ) {
363
384
this . generateArrayHandlerMethods ( "this.value" , type . classReference ! . typeArguments ! [ 0 ] ) ;
364
385
} else {
@@ -367,6 +388,25 @@ export class NEARBindingsBuilder extends ExportsWalker {
367
388
this . sb . push ( "}\n" ) ;
368
389
}
369
390
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
+
370
410
private generateDecodeFunction ( type : Type ) {
371
411
if ( ! type . classReference ) {
372
412
return ;
@@ -390,7 +430,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
390
430
}
391
431
392
432
this . sb . push ( `export function __near_decode_${ typeName } (
393
- buffer: Uint8Array, state: DecoderState): ${ type } {
433
+ buffer: Uint8Array, state: DecoderState):${ this . wrappedTypeName ( type ) } {
394
434
let handler = new __near_JSONHandler_${ typeName } ();
395
435
handler.buffer = buffer;
396
436
handler.decoder = new JSONDecoder<__near_JSONHandler_${ typeName } >(handler);
@@ -444,23 +484,17 @@ export class NEARBindingsBuilder extends ExportsWalker {
444
484
return < Field [ ] > [ ...element . members . values ( ) ] . filter ( member => member instanceof Field ) ;
445
485
}
446
486
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
-
459
487
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 = [ `
461
494
import { near } from "./near";
462
495
import { JSONEncoder} from "./json/encoder"
463
496
import { JSONDecoder, ThrowingJSONHandler, DecoderState } from "./json/decoder"
497
+ import {${ allImportsStr } } from "./${ mainSource . normalizedPath . replace ( ".ts" , "" ) } ";
464
498
465
499
// Runtime functions
466
500
@external("env", "return_value")
@@ -469,11 +503,10 @@ export class NEARBindingsBuilder extends ExportsWalker {
469
503
declare function input_read_len(): u32;
470
504
@external("env", "input_read_into")
471
505
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
+ } )
477
510
return this . sb . join ( "\n" ) ;
478
511
}
479
512
}
0 commit comments