* Add new global function defineLibraryScope(string, boolean). This creates a new global

property object with the name in argument 1 only if it doesn't exist yet, and optionally
  populates it with the standard JavaScript object (String, Date, isNaN, ...)
This commit is contained in:
hns 2005-08-30 16:52:30 +00:00
parent 05bce5953e
commit 5c804670cb

View file

@ -67,7 +67,7 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
"encodeXml", "encodeForm", "stripTags", "formatParagraphs",
"getXmlDocument", "getHtmlDocument", "seal",
"getDBConnection", "getURL", "write", "writeln",
"serialize", "deserialize"
"serialize", "deserialize", "defineLibraryScope"
};
defineFunctionProperties(globalFuncs, GlobalObject.class, 0);
@ -365,6 +365,34 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
return null;
}
/**
* Creates a global object, optionally initializing it with standard
* JavaScript objects. This can be used for script libraries that want
* to extend standard JavaScript functionality, but leave the original
* prototypes alone.
*
* @param name the name of the new scope
*/
public void defineLibraryScope(final String name, boolean initStandardObjects) {
Object obj = get(name, this);
if (obj != NOT_FOUND) {
// put the property again to fool PropertyRecorder
// into believing it has been renewed
put(name, this, obj);
return;
}
ScriptableObject scope = new ScriptableObject() {
public String getClassName() {
return name;
}
};
if (initStandardObjects) {
Context cx = Context.getCurrentContext();
cx.initStandardObjects(scope, false);
}
put(name, this, scope);
}
/**
*
*