Skip to content

Commit

Permalink
Implements system.properties, fixes #456 (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
botic authored May 20, 2022
1 parent 62d04aa commit 15f002d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
7 changes: 6 additions & 1 deletion modules/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,14 @@ Object.defineProperty(this, "global", { value: this });
*/

/**
* The `environment` object contains the Java system properties.
* The `environment` object contains the Java system properties and is specific to the Rhino engine.
* Java system properties are set on the command line using the `-Dproperty=value` syntax and
* can be modified during the runtime. They do not relate to the operating system's environment variables.
* Because of the misleading name of this global property, using [system.properties](../system#properties)
* is recommended.
* @name environment
* @see <a href="http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html">Java System Properties</a>
* @deprecated Use the `system` module's `properties` and `env` exports instead.
*/

/**
Expand Down
26 changes: 25 additions & 1 deletion modules/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,34 @@ exports.args = global.arguments || [];
* LOCALAPPDATA: 'C:\Local',
* ...
* }
* @see <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getenv()">java.lang.System.getenv()</a>
* @see <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/System.html#getenv()">java.lang.System.getenv()</a>
*/
exports.env = new ScriptableMap(System.getenv());

/**
* @name properties
* Returns an unmodifiable view on the current Java system properties.
* @example {
* gopherProxySet: 'false',
* awt.toolkit: 'sun.lwawt.macosx.LWCToolkit',
* java.specification.version: '11',
* sun.cpu.isalist: '',
* sun.jnu.encoding: 'UTF-8',
* java.class.path: '/Users/nobody/ringojs/run.jar',
* java.vm.vendor: 'Eclipse Adoptium',
* sun.arch.data.model: '64',
* java.vendor.url: 'https://adoptium.net/',
* user.timezone: 'Europe/Vienna',
* ...
* }
* @see <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/System.html#getProperties()">java.lang.System.getProperties()</a>
*/
Object.defineProperty(exports, "properties", {
get: () => {
return new ScriptableMap(java.util.Collections.unmodifiableMap(System.getProperties()));
}
});

/**
* Terminates the current process.
* @param {Number} status The exit status, defaults to 0.
Expand Down
4 changes: 3 additions & 1 deletion src/org/ringojs/engine/RingoGlobal.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ public void quit(Context cx, int exitCode) {
ScriptableObject.DONTENUM);
defineProperty("require", new Require(engine, this), DONTENUM);
defineProperty("arguments", cx.newArray(this, engine.getArguments()), DONTENUM);

// Set up "environment" in the global scope to provide access to the
// System environment variables. http://github.com/ringo/ringojs/issues/#issue/88
// System environment variables. https://github.com/ringo/ringojs/issues/88
// DEPRECATED beginning with Ringo 4.x: https://github.com/ringo/ringojs/issues/456
Environment.defineClass(this);
Environment environment = new Environment(this);
defineProperty("environment", environment, ScriptableObject.DONTENUM);
Expand Down
1 change: 1 addition & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exports.testRepository = require('./repository/all');
exports.testIo = require('./io_test');
exports.testModules = require('./modules/all');
exports.testRhino = require("./rhino/all");
exports.testSystem = require('./system_test');

// Also include integration tests.
exports.testIntegration = require('./integration-tests/all');
Expand Down
33 changes: 33 additions & 0 deletions test/system_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const system = require("system");
const assert = require("assert");

exports.testEnv = function() {
assert.throws(() => {
system.env.foo = "bar";
});
assert.throws(() => {
system.env.foo = {};
});
}

exports.testProperties = function() {
assert.throws(() => {
system.properties.foo = "bar";
});
assert.throws(() => {
system.properties.foo = {};
});

assert.isTrue(typeof system.properties["java.version"] === "string");

assert.isUndefined(system.properties["system.test"]);
java.lang.System.setProperty("system.test", "running");
assert.strictEqual(system.properties["system.test"], "running");
java.lang.System.clearProperty("system.test");
assert.isUndefined(system.properties["system.test"]);
};

if (require.main === module) {
system.exit(require("test").run.apply(null,
[exports].concat(system.args.slice(1))));
}

0 comments on commit 15f002d

Please sign in to comment.