@@ -1022,7 +1022,7 @@ public void init() { // not in constructor because it has to be on Runnable.run(
1022
1022
runtime .magicVariables .forEach ((k , v ) -> {
1023
1023
// even hidden variables may need pre-processing
1024
1024
// for e.g. the __arg may contain functions that originated in a different js context
1025
- recurseAndAttach (v , seen );
1025
+ recurseAndAttach (k , v , seen );
1026
1026
setHiddenVariable (k , v );
1027
1027
});
1028
1028
attachVariables (seen ); // re-hydrate any functions from caller or background
@@ -1048,76 +1048,51 @@ public void init() { // not in constructor because it has to be on Runnable.run(
1048
1048
1049
1049
private void attachVariables (Set <Object > seen ) {
1050
1050
vars .forEach ((k , v ) -> {
1051
- switch (v .type ) {
1052
- case JS_FUNCTION :
1053
- Value value = attach (v .getValue ());
1054
- v = new Variable (value );
1055
- vars .put (k , v );
1056
- break ;
1057
- case MAP :
1058
- case LIST :
1059
- recurseAndAttach (v .getValue (), seen );
1060
- break ;
1061
- case OTHER :
1062
- if (v .isJsFunctionWrapper ()) {
1063
- JsFunction jf = v .getValue ();
1064
- Value attached = attachSource (jf .source );
1065
- v = new Variable (attached );
1066
- vars .put (k , v );
1067
- }
1068
- break ;
1069
- default :
1070
- // do nothing
1051
+ Object o = recurseAndAttach (k , v .getValue (), seen );
1052
+ if (o == null ) {
1053
+ o = v .getValue ();
1054
+ } else {
1055
+ try {
1056
+ vars .put (k , new Variable (o ));
1057
+ } catch (Exception e ) {
1058
+ logger .debug ("[*** attach variables ***] failed to attach graal value: {} - {}" , k , e .getMessage ());
1059
+ }
1071
1060
}
1072
- JS .put (k , v . getValue () );
1061
+ JS .put (k , o );
1073
1062
});
1074
1063
}
1075
1064
1076
1065
protected Map <String , Variable > detachVariables () {
1077
1066
Set <Object > seen = Collections .newSetFromMap (new IdentityHashMap ());
1078
1067
Map <String , Variable > detached = new HashMap (vars .size ());
1079
1068
vars .forEach ((k , v ) -> {
1080
- switch (v .type ) {
1081
- case JS_FUNCTION :
1082
- JsFunction jf = new JsFunction (v .getValue ());
1083
- v = new Variable (jf );
1084
- break ;
1085
- case MAP :
1086
- case LIST :
1087
- Object o = recurseAndDetachAndDeepClone (v .getValue (), seen );
1088
- v = new Variable (o );
1089
- break ;
1090
- default :
1091
- // do nothing
1092
- }
1093
- detached .put (k , v );
1069
+ Object o = recurseAndDetachAndDeepClone (v .getValue (), seen );
1070
+ detached .put (k , new Variable (o ));
1094
1071
});
1095
1072
return detached ;
1096
1073
}
1097
1074
1098
1075
// only called by "call" routine
1099
- protected void recurseAndAttach (Object o ) {
1076
+ protected void recurseAndAttach (String name , Object o ) {
1100
1077
Set <Object > seen = Collections .newSetFromMap (new IdentityHashMap ());
1101
- synchronized (runtime .featureRuntime .suite ) {
1102
- recurseAndAttach (o , seen );
1103
- }
1078
+ recurseAndAttach (name , o , seen );
1104
1079
}
1105
1080
1106
- private Object recurseAndAttach (Object o , Set <Object > seen ) {
1081
+ private Object recurseAndAttach (String name , Object o , Set <Object > seen ) {
1107
1082
if (o instanceof Value ) {
1108
1083
Value value = Value .asValue (o );
1109
1084
try {
1110
1085
if (value .canExecute ()) {
1111
1086
if (value .isMetaObject ()) { // js function
1112
1087
return attach (value );
1113
1088
} else { // java function
1114
- return new JsExecutable ( value ) ;
1089
+ return value ;
1115
1090
}
1116
1091
} else { // anything else, including java-type references
1117
1092
return value ;
1118
1093
}
1119
1094
} catch (Exception e ) {
1120
- logger .warn ("[attach] failed to attach js value: {}" , e .getMessage ());
1095
+ logger .warn ("[*** attach *** ] failed to attach js value: {} - {}" , name , e .getMessage ());
1121
1096
return null ;
1122
1097
}
1123
1098
} else if (o instanceof JsFunction ) {
@@ -1129,7 +1104,7 @@ private Object recurseAndAttach(Object o, Set<Object> seen) {
1129
1104
int count = list .size ();
1130
1105
for (int i = 0 ; i < count ; i ++) {
1131
1106
Object child = list .get (i );
1132
- Object result = recurseAndAttach (child , seen );
1107
+ Object result = recurseAndAttach (name + "[" + i + "]" , child , seen );
1133
1108
if (result != null ) {
1134
1109
list .set (i , result );
1135
1110
}
@@ -1140,7 +1115,7 @@ private Object recurseAndAttach(Object o, Set<Object> seen) {
1140
1115
if (seen .add (o )) {
1141
1116
Map <String , Object > map = (Map ) o ;
1142
1117
map .forEach ((k , v ) -> {
1143
- Object result = recurseAndAttach (v , seen );
1118
+ Object result = recurseAndAttach (name + "." + k , v , seen );
1144
1119
if (result != null ) {
1145
1120
map .put (k , result );
1146
1121
}
@@ -1162,11 +1137,10 @@ protected Object recurseAndAttachAndDeepClone(Object o) {
1162
1137
1163
1138
private Object recurseAndAttachAndDeepClone (Object o , Set <Object > seen ) {
1164
1139
if (o instanceof Value ) {
1165
- // will happen only for java "class" and java functions (static methods)
1166
1140
try {
1167
1141
return Value .asValue (o );
1168
1142
} catch (Exception e ) {
1169
- logger .warn ("[attach deep] failed to attach graal value: {}" , e .getMessage ());
1143
+ logger .warn ("[*** attach deep *** ] failed to attach graal value: {}" , e .getMessage ());
1170
1144
return null ;
1171
1145
}
1172
1146
}
@@ -1206,20 +1180,20 @@ protected Object recurseAndDetachAndDeepClone(Object o) {
1206
1180
1207
1181
private Object recurseAndDetachAndDeepClone (Object o , Set <Object > seen ) {
1208
1182
if (o instanceof Value ) {
1209
- Value value = Value . asValue ( o ) ;
1183
+ Value value = ( Value ) o ;
1210
1184
try {
1211
1185
if (value .canExecute ()) {
1212
1186
if (value .isMetaObject ()) { // js function
1213
- o = new JsFunction (value );
1187
+ return new JsFunction (value );
1214
1188
} else { // java function
1215
- o = new JsExecutable (value );
1189
+ return new JsExecutable (value );
1216
1190
}
1217
1191
} else {
1218
1192
// everything else including java-type references that do not need special attach handling
1219
- o = value ;
1193
+ o = JsValue . toJava ( value ) ;
1220
1194
}
1221
1195
} catch (Exception e ) {
1222
- logger .warn ("[detach] unsupported value in callonce / callSingle: {}" , e .getMessage ());
1196
+ logger .warn ("[*** detach deep *** ] unsupported value in callonce / callSingle: {}" , e .getMessage ());
1223
1197
return null ;
1224
1198
}
1225
1199
}
@@ -1327,7 +1301,13 @@ public void setVariables(Map<String, Object> map) {
1327
1301
if (map == null ) {
1328
1302
return ;
1329
1303
}
1330
- map .forEach ((k , v ) -> setVariable (k , v ));
1304
+ map .forEach ((k , v ) -> {
1305
+ try {
1306
+ setVariable (k , v );
1307
+ } catch (Exception e ) {
1308
+ logger .warn ("[*** set variables ***] skipping invalid graal value: {} - {}" , k , e .getMessage ());
1309
+ }
1310
+ });
1331
1311
}
1332
1312
1333
1313
private static Map <String , Variable > copy (Map <String , Variable > source , boolean deep ) {
@@ -1953,7 +1933,7 @@ public Variable call(Variable called, Variable arg, boolean sharedScope) {
1953
1933
case FEATURE :
1954
1934
// will be always a map or a list of maps (loop call result)
1955
1935
Object callResult = callFeature (called .getValue (), arg , -1 , sharedScope );
1956
- recurseAndAttach (callResult );
1936
+ recurseAndAttach ("" , callResult );
1957
1937
return new Variable (callResult );
1958
1938
default :
1959
1939
throw new RuntimeException ("not a callable feature or js function: " + called );
0 commit comments