Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES5 Object not accessible when used in commonjs module #95

Closed
dsolimando opened this issue Nov 5, 2012 · 2 comments
Closed

ES5 Object not accessible when used in commonjs module #95

dsolimando opened this issue Nov 5, 2012 · 2 comments
Labels
CommonJS Issues related to the CommonJS implementation in Rhino Triage Issues yet to be triaged

Comments

@dsolimando
Copy link

This code:

Object.defineProperties(obj, {
"property1": { value: true, writable: true },
"property2": { value: "Hello", writable: false}
});

will fail with the following exception:

org.mozilla.javascript.EcmaError: ReferenceError: "Object" is not defined.

@p-bakker p-bakker added the CommonJS Issues related to the CommonJS implementation in Rhino label Jun 29, 2021
@p-bakker p-bakker added the Triage Issues yet to be triaged label Oct 14, 2021
@szegedi
Copy link
Contributor

szegedi commented Feb 8, 2023

Works for me:

import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.commonjs.module.Require;
import org.mozilla.javascript.commonjs.module.RequireBuilder;
import org.mozilla.javascript.commonjs.module.provider.ModuleSource;
import org.mozilla.javascript.commonjs.module.provider.ModuleSourceProvider;
import org.mozilla.javascript.commonjs.module.provider.StrongCachingModuleScriptProvider;

public class Main2 {
    public static void main(String[] args) {
        try(Context cx = Context.enter()) {
            cx.setOptimizationLevel(-1);
            Scriptable scope = cx.initStandardObjects();
            Require r = new RequireBuilder()
                .setModuleScriptProvider(new StrongCachingModuleScriptProvider(
                    new ModuleSourceProvider() {
                        @Override public ModuleSource loadSource(String moduleId, Scriptable paths,
                            Object validator) throws URISyntaxException {
                            var base = new URI("http://localhost/");
                            return loadSource(base.resolve(moduleId), base, validator);
                        }

                        @Override
                        public ModuleSource loadSource(URI uri, URI baseUri, Object validator) {
                            return new ModuleSource(
                                new StringReader(
                                    "var obj={}; Object.defineProperties(obj, {\n"
                                     + "\"property1\": { value: true, writable: true },\n"
                                     + "\"property2\": { value: \"Hello\", writable: false}\n"
                                     + "});"), null, uri, baseUri, validator);
                        }
                    })
                )
                .createRequire(cx, scope);
            r.install(scope);
            cx.evaluateString(scope, "require('whatever.js')", "", 1, null);
        }
    }
}

This programs exits without errors. Try to introduce a typo in "definePropertes" and you'll see it fails (so it does work without the typo.)

@p-bakker
Copy link
Collaborator

p-bakker commented Feb 8, 2023

Closing as cannot be reproduced. Feel free to reopen with a reproducible example

@p-bakker p-bakker closed this as completed Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CommonJS Issues related to the CommonJS implementation in Rhino Triage Issues yet to be triaged
Projects
None yet
Development

No branches or pull requests

3 participants