@@ -194,6 +194,8 @@ const NativeUIntArg = nativeArgs.NativeUIntArg;
194
194
const NativeNumArg = nativeArgs . NativeNumArg ;
195
195
const NativeStrArg = nativeArgs . NativeStrArg ;
196
196
197
+ const NativeNumRet = nativeArgs . NativeNumRet ;
198
+
197
199
const intToObj = exports . intToObj = function ( currentHLL , i ) {
198
200
const type = currentHLL . get ( 'int_box' ) ;
199
201
if ( ! type ) {
@@ -615,28 +617,87 @@ class WrappedFunction extends NQPObject {
615
617
for ( let i = 2 ; i < args . length ; i ++ ) {
616
618
converted . push ( /*await*/ toJS ( args [ i ] ) ) ;
617
619
}
618
- return fromJS ( this . func . apply ( null , converted ) ) ;
620
+ return fromJSToReturnValue ( args [ 0 ] , this . func . apply ( null , converted ) ) ;
619
621
}
620
622
621
623
$$call ( args ) {
622
624
return this . $$apply ( arguments ) ;
623
625
}
624
626
} ;
625
627
626
- function fromJS ( obj ) {
628
+ function fromJSToReturnValue ( ctx , obj ) {
629
+ return fromJS ( ctx . $$getHLL ( ) , obj , true , false ) ;
630
+ }
631
+
632
+ exports . fromJSToReturnValue = fromJSToReturnValue ;
633
+
634
+ function fromJSToArgument ( obj ) {
635
+ return fromJS ( hll . getHLL ( 'perl6' ) , obj , false , true ) ;
636
+ }
637
+
638
+ function fromJSToObject ( ctx , obj ) {
639
+ return fromJS ( hll . getHLL ( 'perl6' ) , obj , false , false ) ;
640
+ }
641
+
642
+ function fromJS ( HLL , obj , isReturnValue , isArgument ) {
627
643
if ( typeof obj === 'function' ) {
628
644
return new WrappedFunction ( obj ) ;
629
645
} else if ( obj === undefined || obj === null ) {
630
- return Null ;
646
+ return HLL . get ( 'null_value' ) ;
647
+ } else if ( obj === true ) {
648
+ return HLL . get ( 'true_value' ) ;
649
+ } else if ( obj === false ) {
650
+ return HLL . get ( 'false_value' ) ;
631
651
} else if ( typeof obj === 'number' ) {
632
- return new NQPNum ( obj ) ;
652
+ if ( isReturnValue ) {
653
+ return new NativeNumRet ( obj ) ;
654
+ } else if ( isArgument ) {
655
+ return new NativeNumArg ( obj ) ;
656
+ } else {
657
+ const type = HLL . get ( 'int_box' ) ;
658
+ const boxed = type . _STable . REPR . allocate ( type . _STable ) ;
659
+ boxed . $$setInt ( obj ) ;
660
+ return boxed ;
661
+ }
633
662
} else if ( typeof obj === 'string' ) {
634
- return new NQPStr ( obj ) ;
635
- } else {
663
+ if ( isReturnValue ) {
664
+ return obj ;
665
+ } else if ( isArgument ) {
666
+ return new NativeStrArg ( obj ) ;
667
+ } else {
668
+ const type = HLL . get ( 'str_box' ) ;
669
+ const boxed = type . _STable . REPR . allocate ( type . _STable ) ;
670
+ boxed . $$setStr ( obj ) ;
671
+ return boxed ;
672
+ }
673
+ } else if ( obj . $$decont ) {
674
+ return obj ;
675
+ } else if ( obj instanceof EvalResult ) {
636
676
return obj ;
677
+ } else {
678
+ const type = HLL . get ( 'js_box' ) ;
679
+ const wrapped = type . _STable . REPR . allocate ( type . _STable ) ;
680
+ wrapped . $$jsObject = obj ;
681
+ return wrapped ;
682
+ }
683
+ }
684
+
685
+ function toJSWithCtx ( ctx , obj ) {
686
+ const HLL = ctx . $$getHLL ( ) ;
687
+ if ( obj . $$istype ( ctx , HLL . get ( 'str_box' ) ) ) {
688
+ return obj . $$getStr ( ) ;
689
+ } else if ( obj . $$istype ( ctx , HLL . get ( 'num_box' ) ) ) {
690
+ return obj . $$getNum ( ) ;
691
+ } else if ( obj . $$istype ( ctx , HLL . get ( 'js_box' ) ) ) {
692
+ return obj . $$jsObject ;
693
+ } else {
694
+ require ( 'nqp-runtime' ) . dumpObj ( obj ) ;
695
+ console . trace ( `Can't unbox` ) ;
637
696
}
638
697
}
639
698
699
+ exports . toJSWithCtx = toJSWithCtx ;
700
+
640
701
function toJS ( obj ) {
641
702
if ( obj instanceof NativeIntArg || obj instanceof NativeUIntArg ) {
642
703
return obj . value ;
@@ -650,7 +711,7 @@ function toJS(obj) {
650
711
return function ( ) {
651
712
const converted = [ null , { } ] ;
652
713
for ( let i = 0 ; i < arguments . length ; i ++ ) {
653
- converted . push ( fromJS ( arguments [ i ] ) ) ;
714
+ converted . push ( fromJSToArgument ( arguments [ i ] ) ) ;
654
715
}
655
716
return toJS ( obj . $$apply ( converted ) ) ;
656
717
} ;
@@ -796,7 +857,8 @@ class JavaScriptCompiler extends NQPObject {
796
857
797
858
eval ( ctx , _NAMED , self , code ) {
798
859
if ( ! ( _NAMED !== null && _NAMED . hasOwnProperty ( 'mapping' ) ) ) {
799
- return fromJS ( eval ( nqp . arg_s ( ctx , code ) ) ) ;
860
+ let codeStr = nqp . arg_s ( ctx , code ) ;
861
+ return fromJSToReturnValue ( ctx , eval ( codeStr ) ) ;
800
862
}
801
863
802
864
const fakeFilename = 'nqpEval' + shortid . generate ( ) ;
@@ -840,7 +902,7 @@ class JavaScriptCompiler extends NQPObject {
840
902
return require ( path ) ;
841
903
} ;
842
904
843
- const ret = fromJS ( compiled ( ) ) ;
905
+ const ret = fromJSToReturnValue ( ctx , compiled ( ) ) ;
844
906
global . nqpRequire = oldNqpRequire ;
845
907
846
908
return ret ;
@@ -854,7 +916,7 @@ class JavaScriptCompiler extends NQPObject {
854
916
855
917
const codeRef = new CodeRef ( ) ;
856
918
codeRef . $$call = function ( ctx , _NAMED ) {
857
- return fromJS ( compiled ( ) ) ;
919
+ return fromJSToReturnValue ( ctx , compiled ( ) ) ;
858
920
} ;
859
921
return codeRef ;
860
922
}
@@ -1994,3 +2056,11 @@ op.iseq_snfg = function(a, b) {
1994
2056
op . isne_snfg = function ( a , b ) {
1995
2057
return ( a . normalize ( 'NFC' ) === b . normalize ( 'NFC' ) ) ? 0 : 1 ;
1996
2058
} ;
2059
+
2060
+ op . setjsattr = function ( ctx , obj , attr , value ) {
2061
+ return obj . $$jsObject [ attr ] = toJSWithCtx ( ctx , value ) ;
2062
+ } ;
2063
+
2064
+ op . getjsattr = function ( ctx , obj , attr ) {
2065
+ return fromJSToObject ( ctx , obj . $$jsObject [ attr ] ) ;
2066
+ } ;
0 commit comments