-
Hello, the past few days I was wondering if Rhino provides a way to put a value into the scope, which will serve as a placeholder until the moment it is accessed. This would be used to lazily load the value on which the access will then be performed. A (nonsensical) example showing how this transparent placeholder should work: try (Context ctx = Context.enter()) {
Scriptable scope = ctx.initStandardObjects();
ctx.evaluateString(scope,
"(function() {" +
" var value = LazyProvider.lazyLoad(123); // value will be a placeholder Scriptable returned by the function call" +
" // ..." +
" var x = value.toString(); // load value here, toString() will then be called on e.g. the number 123" +
" return x;" +
"})()",
"", 0, null);
} I've tried to get something like that to work with the provided Rhino interfaces, but at this point I'm not sure if a way to implement such a functionality is actually provided by Rhino. If there is already a way to do something like that, a hint would be greatly appreciated. In case there isn't, I've implemented a rudimentary TransparentProxy class which adds this functionality to Rhino. It would be very nice if someone could take a glance at the commit, given that I'm not really versed in the Rhino codebase and that this was just quickly hacked together. So there are probably a few things I'm missing or haven't considered. The commit implementing the TransparentProxy is located here: erstr2ggl@cfe8aac Thank you very much in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In plain JavaScript if one wants to lazyload an object only when it's accessed, one would define a property with a get method on it that would 'fetch' the object and return it only when the property is accessed (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get for example) I think you can do the same in Rhino in various ways |
Beta Was this translation helpful? Give feedback.
In plain JavaScript if one wants to lazyload an object only when it's accessed, one would define a property with a get method on it that would 'fetch' the object and return it only when the property is accessed (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get for example)
I think you can do the same in Rhino in various ways