@@ -262,7 +262,10 @@ function compileObjectInitializer(obj: ObjectBinding) {
262262 }
263263 switch ( init . kind ) {
264264 case SyntaxKind . StringLiteral :
265- return `strdup(${ init . text } )` ;
265+ if ( init . text === 'NULL' ) {
266+ return 'NULL' ;
267+ }
268+ return `strdup2(${ init . text } )` ;
266269 case SyntaxKind . NumericLiteral :
267270 return init . text ;
268271 case SyntaxKind . NewExpression :
@@ -401,52 +404,48 @@ function compileComponentEventHandlers(ctx: ComponentContext) {
401404 ] . join ( "\n\n" ) ;
402405}
403406
404- function compileComponentMethods ( ctx : ComponentContext ) {
405- return [
406- `static void ${ ctx . name } _react_init(ui_widget_t *w)` ,
407- "{" ,
408- ` ${ ctx . name } _react_t *_that = ui_widget_get_data(w, ${ ctx . name } _proto);` ,
409- ` ${ ctx . name } _load_template(w, &_that->refs);` ,
410- ` ${ ctx . name } _react_init_state(w);` ,
411- ` ${ ctx . name } _react_init_events(w);` ,
412- ` ${ ctx . name } _react_update(w);` ,
413- "}\n" ,
414- `static void ${ ctx . name } _react_destroy(ui_widget_t *w)` ,
415- "{" ,
416- ` ${ ctx . name } _react_destroy_state(w);` ,
417- "}\n" ,
418- compileComponentMethod ( { ctx, name : "react_update" } ) ,
419- ] . join ( "\n" ) ;
420- }
421-
422407function compileTypes ( ctx : ComponentContext ) {
423408 return [
424- " typedef struct {" ,
409+ ` typedef struct ${ ctx . name } _react_state {` ,
425410 ...ctx . state . map (
426411 ( item ) =>
427412 ` ${ getObjectTypeName ( item . initializer ) } ${ item . identifier } ;`
428413 ) ,
429414 `} ${ ctx . name } _react_state_t;` ,
430- '' ,
431- ' typedef struct {' ,
415+ "" ,
416+ ` typedef struct ${ ctx . name } _react {` ,
432417 ` ${ ctx . name } _react_state_t state;` ,
433418 ` ${ ctx . name } _refs_t refs;` ,
434- `} ${ ctx . name } _react_t;`
435- ] . join ( '\n' ) ;
419+ `} ${ ctx . name } _react_t;` ,
420+ ] . join ( "\n" ) ;
436421}
437422
438423function compileComponent ( ctx : ComponentContext ) {
439424 return [
440- compiler . compileComponentState ( ctx ) ,
441- compiler . compileComponentEventHandlers ( ctx ) ,
442- compiler . compileComponentMethods ( ctx ) ,
425+ compileComponentState ( ctx ) ,
426+ compileComponentMethod ( { ctx, name : "react_update" } ) ,
427+ compileComponentEventHandlers ( ctx ) ,
428+ [
429+ `static void ${ ctx . name } _react_init(ui_widget_t *w)` ,
430+ "{" ,
431+ ` ${ ctx . name } _react_t *_that = ui_widget_get_data(w, ${ ctx . name } _proto);` ,
432+ ` ${ ctx . name } _load_template(w, &_that->refs);` ,
433+ ` ${ ctx . name } _react_init_state(w);` ,
434+ ` ${ ctx . name } _react_init_events(w);` ,
435+ ` ${ ctx . name } _react_update(w);` ,
436+ "}\n" ,
437+ `static void ${ ctx . name } _react_destroy(ui_widget_t *w)` ,
438+ "{" ,
439+ ` ${ ctx . name } _react_destroy_state(w);` ,
440+ "}" ,
441+ ] . join ( "\n" ) ,
443442 ] . join ( "\n\n" ) ;
444443}
445444
446445let contextList : FunctionContext [ ] = [ ] ;
447446
448447export function getFunctionContext ( ) {
449- if ( contextList . length < 2 ) {
448+ if ( contextList . length < 1 ) {
450449 throw new SyntaxError ( "FunctionContext is missing" ) ;
451450 }
452451 return contextList [ contextList . length - 1 ] ;
@@ -662,10 +661,9 @@ export const compiler = {
662661 compileObjectDestroyer,
663662 compileFunction,
664663 compileComponentState,
665- compileComponentMethods,
666664 compileComponentEventHandlers,
667665 compileComponentMethod,
668- compileComponent
666+ compileComponent,
669667} ;
670668
671669export const factory = {
0 commit comments