@@ -10,6 +10,7 @@ const utils = internal.utils;
10
10
11
11
const public = @import ("../../api.zig" );
12
12
const Loop = public .Loop ;
13
+ const JSObject = public .JSObject ;
13
14
14
15
const cbk = @import ("callback.zig" );
15
16
const nativeToJS = @import ("types_primitives.zig" ).nativeToJS ;
@@ -167,7 +168,6 @@ fn getArg(
167
168
isolate : v8.Isolate ,
168
169
ctx : v8.Context ,
169
170
) arg.T {
170
- _ = this ;
171
171
var value : arg.T = undefined ;
172
172
173
173
if (arg .isNative ()) {
@@ -192,6 +192,7 @@ fn getArg(
192
192
std .mem .Allocator = > alloc ,
193
193
* Loop = > utils .loop ,
194
194
cbk .Func , cbk .FuncSync , cbk .Arg = > unreachable ,
195
+ JSObject = > JSObject { .ctx = ctx , .js_obj = this },
195
196
else = > jsToNative (
196
197
alloc ,
197
198
arg .T ,
@@ -395,7 +396,7 @@ pub fn setNativeObject(
395
396
obj : anytype ,
396
397
js_obj : v8.Object ,
397
398
isolate : v8.Isolate ,
398
- ) ! void {
399
+ ) ! * T {
399
400
400
401
// assign and bind native obj to JS obj
401
402
var obj_ptr : * T = undefined ;
@@ -420,7 +421,7 @@ pub fn setNativeObject(
420
421
// if the object is an empty struct (ie. a kind of container)
421
422
// no need to keep it's reference
422
423
if (T_refl .isEmpty ()) {
423
- return ;
424
+ return obj_ptr ;
424
425
}
425
426
426
427
// bind the native object pointer to JS obj
@@ -436,13 +437,16 @@ pub fn setNativeObject(
436
437
try refs .addObject (alloc , int_ptr .* , T_refl .index );
437
438
}
438
439
js_obj .setInternalField (0 , ext );
440
+ return obj_ptr ;
439
441
}
440
442
441
443
fn setReturnType (
442
444
alloc : std.mem.Allocator ,
443
445
comptime all_T : []refl.Struct ,
444
446
comptime ret : refl.Type ,
447
+ comptime func : refl.Func ,
445
448
res : anytype ,
449
+ js_res : v8.ReturnValue ,
446
450
ctx : v8.Context ,
447
451
isolate : v8.Isolate ,
448
452
) ! v8.Value {
@@ -454,7 +458,7 @@ fn setReturnType(
454
458
// if null just return JS null
455
459
return isolate .initNull ().toValue ();
456
460
}
457
- return setReturnType (alloc , all_T , ret , res .? , ctx , isolate );
461
+ return setReturnType (alloc , all_T , ret , func , res .? , js_res , ctx , isolate );
458
462
}
459
463
460
464
// Union type
@@ -468,7 +472,9 @@ fn setReturnType(
468
472
alloc ,
469
473
all_T ,
470
474
tt ,
475
+ func ,
471
476
@field (res , tt .name .? ),
477
+ js_res ,
472
478
ctx ,
473
479
isolate ,
474
480
);
@@ -490,7 +496,9 @@ fn setReturnType(
490
496
alloc ,
491
497
all_T ,
492
498
field ,
499
+ func ,
493
500
@field (res , name ),
501
+ js_res ,
494
502
ctx ,
495
503
isolate ,
496
504
);
@@ -509,14 +517,32 @@ fn setReturnType(
509
517
// instantiate a JS object from template
510
518
// and bind it to the native object
511
519
const js_obj = gen .getTpl (index ).tpl .getInstanceTemplate ().initInstance (ctx );
512
- _ = try setNativeObject (
520
+ const obj_ptr = setNativeObject (
513
521
alloc ,
514
522
all_T [index ],
515
523
ret .underT (),
516
524
res ,
517
525
js_obj ,
518
526
isolate ,
519
- );
527
+ ) catch unreachable ;
528
+
529
+ // call postAttach func
530
+ const T_refl = all_T [index ];
531
+ if (comptime try refl .postAttachFunc (T_refl .T )) | piArgsT | {
532
+ postAttach (
533
+ utils .allocator ,
534
+ T_refl ,
535
+ all_T ,
536
+ func ,
537
+ piArgsT ,
538
+ obj_ptr ,
539
+ js_obj ,
540
+ js_res ,
541
+ ctx ,
542
+ isolate ,
543
+ );
544
+ }
545
+
520
546
return js_obj .toValue ();
521
547
}
522
548
@@ -530,6 +556,44 @@ fn setReturnType(
530
556
return js_val ;
531
557
}
532
558
559
+ fn postAttach (
560
+ alloc : std.mem.Allocator ,
561
+ comptime T_refl : refl.Struct ,
562
+ comptime all_T : []refl.Struct ,
563
+ comptime func : refl.Func ,
564
+ comptime argsT : type ,
565
+ obj_ptr : anytype ,
566
+ js_obj : v8.Object ,
567
+ js_res : v8.ReturnValue ,
568
+ ctx : v8.Context ,
569
+ isolate : v8.Isolate ,
570
+ ) void {
571
+ var args : argsT = undefined ;
572
+ @field (args , "0" ) = obj_ptr ;
573
+ @field (args , "1" ) = JSObject { .ctx = ctx , .js_obj = js_obj };
574
+ const f = @field (T_refl .T , "postAttach" );
575
+ const ret = comptime try refl .funcReturnType (@TypeOf (f ));
576
+ if (comptime refl .isErrorUnion (ret )) {
577
+ _ = @call (.auto , f , args ) catch | err | {
578
+ return throwError (
579
+ alloc ,
580
+ T_refl ,
581
+ all_T ,
582
+ func ,
583
+ err ,
584
+ js_res ,
585
+ isolate ,
586
+ );
587
+ };
588
+ } else {
589
+ _ = @call (
590
+ .auto ,
591
+ f ,
592
+ args ,
593
+ );
594
+ }
595
+ }
596
+
533
597
fn getNativeObject (
534
598
comptime T_refl : refl.Struct ,
535
599
comptime all_T : []refl.Struct ,
@@ -639,22 +703,40 @@ fn callFunc(
639
703
if (comptime func_kind == .constructor ) {
640
704
641
705
// bind native object to JS object this
642
- setNativeObject (
706
+ _ = setNativeObject (
643
707
utils .allocator ,
644
708
T_refl ,
645
709
func .return_type .underT (),
646
710
res ,
647
711
cbk_info .getThis (),
648
712
isolate ,
649
713
) catch unreachable ; // TODO: internal errors
714
+
715
+ // call postAttach func
716
+ if (comptime try refl .postAttachFunc (T_refl .T )) | piArgsT | {
717
+ postAttach (
718
+ utils .allocator ,
719
+ T_refl ,
720
+ all_T ,
721
+ func ,
722
+ piArgsT ,
723
+ & res ,
724
+ cbk_info .getThis (),
725
+ cbk_info .getReturnValue (),
726
+ ctx ,
727
+ isolate ,
728
+ );
729
+ }
650
730
} else {
651
731
652
732
// return to javascript the result
653
733
const js_val = setReturnType (
654
734
utils .allocator ,
655
735
all_T ,
656
736
func .return_type ,
737
+ func ,
657
738
res ,
739
+ cbk_info .getReturnValue (),
658
740
ctx ,
659
741
isolate ,
660
742
) catch unreachable ; // TODO: internal errors
0 commit comments