@@ -66,10 +66,10 @@ public class RScope extends RTruffleBaseObject {
6666 private final int currentScopeOffset ;
6767
6868 /**
69- * Cached array and hash set of the local names of the current scope, initialized lazily.
69+ * Cached collections with the local names of the current scope, initialized lazily.
7070 */
7171 private volatile List <String > currentNames ;
72- private volatile String [] currentNamesArray ;
72+ private volatile List < String > currentNamesOnlyPublic ;
7373
7474 public RScope (REnvironment env , REnvFrameAccess frameAccess , RootNode rootNode ) {
7575 assert frameAccess != null ;
@@ -97,6 +97,7 @@ public boolean isScope() {
9797 }
9898
9999 @ ExportMessage
100+ @ SuppressWarnings ("static-method" )
100101 final boolean hasMembers () {
101102 return true ;
102103 }
@@ -121,7 +122,7 @@ boolean isMemberInsertable(String member) {
121122 @ TruffleBoundary
122123 boolean isMemberModifiable (String member ) {
123124 for (int i = currentScopeOffset ; i < scopesChain .length ; i ++) {
124- if (scopesChain [i ].getCurrentNames ().contains (member )) {
125+ if (scopesChain [i ].getCurrentNames (true ).contains (member )) {
125126 return !frameAccess .bindingIsLocked (member );
126127 }
127128 }
@@ -150,7 +151,7 @@ public boolean hasMemberWriteSideEffects(String member) {
150151 @ TruffleBoundary
151152 private boolean exists (String member ) {
152153 for (int i = currentScopeOffset ; i < scopesChain .length ; i ++) {
153- if (scopesChain [i ].getCurrentNames ().contains (member )) {
154+ if (scopesChain [i ].getCurrentNames (true ).contains (member )) {
154155 return true ;
155156 }
156157 }
@@ -198,7 +199,7 @@ final void writeMember(String member, Object value,
198199 if (existingValue != null ) {
199200 try {
200201 // Note: frame access takes care of active bindings
201- scopesChain [i ].frameAccess .put (member , value );
202+ scopesChain [i ].frameAccess .put (member , foreign2R . convert ( value ) );
202203 } catch (PutException e ) {
203204 // locked binding, the member should not have been modifiable/insertable
204205 throw UnsupportedMessageException .create ();
@@ -207,7 +208,7 @@ final void writeMember(String member, Object value,
207208 }
208209 // Not found. By default, we'll insert into the current scope
209210 try {
210- frameAccess .put (member , value );
211+ frameAccess .put (member , foreign2R . convert ( value ) );
211212 } catch (PutException e ) {
212213 throw UnsupportedMessageException .create ();
213214 }
@@ -306,21 +307,19 @@ private static boolean acceptEnv(REnvironment env, boolean localScopes) {
306307 return env != REnvironment .emptyEnv () && !(localScopes && env == REnvironment .globalEnv ());
307308 }
308309
309- private List <String > getCurrentNames () {
310+ private List <String > getCurrentNames (boolean includeInternal ) {
310311 CompilerAsserts .neverPartOfCompilation ();
311- if (currentNames == null ) {
312- currentNames = Arrays .asList (frameAccess .ls (true , null , false ).getReadonlyStringData ());
313- }
314- return currentNames ;
315- }
316-
317- @ TruffleBoundary
318- private String [] getCurrentNamesArray () {
319- if (currentNamesArray == null ) {
320- List <String > currentNames = getCurrentNames ();
321- currentNamesArray = currentNames .toArray (new String [0 ]);
312+ if (includeInternal ) {
313+ if (currentNames == null ) {
314+ currentNames = Arrays .asList (frameAccess .ls (true , null , false ).getReadonlyStringData ());
315+ }
316+ return currentNames ;
317+ } else {
318+ if (currentNamesOnlyPublic == null ) {
319+ currentNamesOnlyPublic = Arrays .asList (frameAccess .ls (false , null , false ).getReadonlyStringData ());
320+ }
321+ return currentNamesOnlyPublic ;
322322 }
323- return currentNamesArray ;
324323 }
325324
326325 @ ExportLibrary (InteropLibrary .class )
@@ -331,11 +330,11 @@ public final class RScopeMembers extends RTruffleBaseObject {
331330
332331 public RScopeMembers (boolean includeInternal ) {
333332 this .includeInternal = includeInternal ;
334- int size = 0 ;
333+ int sizeLocal = 0 ;
335334 for (int i = currentScopeOffset ; i < scopesChain .length ; i ++) {
336- size += scopesChain [i ].getCurrentNamesArray (). length ;
335+ sizeLocal += scopesChain [i ].getCurrentNames ( includeInternal ). size () ;
337336 }
338- this .size = size ;
337+ this .size = sizeLocal ;
339338 }
340339
341340 @ ExportMessage
@@ -358,11 +357,11 @@ public String readArrayElement(long indexL) throws InvalidArrayIndexException {
358357 int currentIndex = 0 ;
359358 int index = (int ) indexL ;
360359 for (int i = currentScopeOffset ; i < scopesChain .length ; i ++) {
361- String [] names = scopesChain [i ].getCurrentNamesArray ( );
362- if (currentIndex + names .length > index ) {
363- return names [ index - currentIndex ] ;
360+ List < String > names = scopesChain [i ].getCurrentNames ( includeInternal );
361+ if (currentIndex + names .size () > index ) {
362+ return names . get ( index - currentIndex ) ;
364363 }
365- currentIndex += names .length ;
364+ currentIndex += names .size () ;
366365 }
367366 throw InvalidArrayIndexException .create (indexL );
368367 }
0 commit comments