<commit_before>package org.projectodd.nodyn;

import org.dynjs.runtime.DynJS;
import org.dynjs.runtime.DynObject;
import org.dynjs.runtime.ExecutionContext;
import org.dynjs.runtime.GlobalObject;
import org.dynjs.runtime.GlobalObjectFactory;
import org.dynjs.runtime.InitializationListener;
import org.dynjs.vertx.DynJSVerticle;
import org.dynjs.vertx.DynJSVerticleFactory;
import org.projectodd.nodyn.util.QueryString;
import org.projectodd.nodyn.buffer.BufferType;
import org.vertx.java.platform.Verticle;

public class NodeJSVerticleFactory extends DynJSVerticleFactory {
    private GlobalObjectFactory globalObjectFactory = new NodeJSGlobalObjectFactory();
    private String filename;

    @Override
    protected GlobalObjectFactory getGlobalObjectFactory() {
        return this.globalObjectFactory;
    }

    @Override
    public Verticle createVerticle(String main) throws Exception {
        this.filename = main;
        return new NodeJSVerticle(this, main);
    }

    private class NodeJSGlobalObjectFactory extends DynJSGlobalObjectFactory {
        @Override
        public GlobalObject newGlobalObject(final DynJS runtime) {
            GlobalObject global = super.newGlobalObject(runtime);
            BufferType bufferType = new BufferType(global);
            DynObject node = new DynObject(global);
            node.put("buffer", bufferType);
            node.put("QueryString", new QueryString(global));
            global.defineGlobalProperty("nodyn", node);
            global.defineGlobalProperty("Buffer", bufferType);
            global.defineGlobalProperty("__filename", filename);
            return global;
        }
    }
    
    protected class NodeJSVerticle extends DynJSVerticle {

        public NodeJSVerticle(DynJSVerticleFactory factory, String scriptName) {
            super(factory, scriptName);
        }
        
        @Override
        protected ExecutionContext initializeRootContext() {
            return ExecutionContext.createGlobalExecutionContext(getRuntime(), new InitializationListener()
            {
                @Override
                public void initialize(ExecutionContext context) {
                    try {
                        getRuntime().evaluate("include('node.js')");
                    } catch (Exception e) {
                        System.err.println("[ERROR] Cannot initialize Nodyn. " + e.getMessage());
                        e.printStackTrace();
                    }
                }
            });
        }

    }<commit_msg>Define process and console in the root context<commit_after>package org.projectodd.nodyn;

import org.dynjs.runtime.DynJS;
import org.dynjs.runtime.DynObject;
import org.dynjs.runtime.ExecutionContext;
import org.dynjs.runtime.GlobalObject;
import org.dynjs.runtime.GlobalObjectFactory;
import org.dynjs.runtime.InitializationListener;
import org.dynjs.vertx.DynJSVerticle;
import org.dynjs.vertx.DynJSVerticleFactory;
import org.projectodd.nodyn.util.QueryString;
import org.projectodd.nodyn.buffer.BufferType;
import org.vertx.java.platform.Verticle;

import java.io.*;

public class NodeJSVerticleFactory extends DynJSVerticleFactory {
    private GlobalObjectFactory globalObjectFactory = new NodeJSGlobalObjectFactory();
    private String filename;

    @Override
    protected GlobalObjectFactory getGlobalObjectFactory() {
        return this.globalObjectFactory;
    }

    @Override
    public Verticle createVerticle(String main) throws Exception {
        this.filename = main;
        return new NodeJSVerticle(this, main);
    }

    private class NodeJSGlobalObjectFactory extends DynJSGlobalObjectFactory {
        @Override
        public GlobalObject newGlobalObject(final DynJS runtime) {
            GlobalObject global = super.newGlobalObject(runtime);
            BufferType bufferType = new BufferType(global);
            DynObject node = new DynObject(global);
            node.put("buffer", bufferType);
            node.put("QueryString", new QueryString(global));
            global.defineGlobalProperty("nodyn", node);
            global.defineGlobalProperty("Buffer", bufferType);
            global.defineGlobalProperty("__filename", filename);
            return global;
        }
    }
    
    protected class NodeJSVerticle extends DynJSVerticle {

        public NodeJSVerticle(DynJSVerticleFactory factory, String scriptName) {
            super(factory, scriptName);
        }
        
        @Override
        protected ExecutionContext initializeRootContext() {
            return ExecutionContext.createGlobalExecutionContext(getRuntime(), new InitializationListener()
            {
                @Override
                public void initialize(ExecutionContext context) {
                    // not sure why this is necessary, but if we just have `process = require('process')` in node.js
                    // it's not loaded globally. We must both define the global property right here, and also
                    // require the file from within node.js - strange.
                    // I still don't really understand how to make something truly global in dynjs
                    GlobalObject globalObject = context.getGlobalObject();
                    globalObject.defineGlobalProperty("process", runtime.newRunner().withContext(context).withSource("require('process')").execute());
                    globalObject.defineGlobalProperty("console", runtime.newRunner().withContext(context).withSource("require('node_console')").execute());
                    InputStream is = runtime.getConfig().getClassLoader().getResourceAsStream("node.js");
                    if (is != null) {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        runtime.newRunner().withSource(reader).evaluate();
                        try {
                            is.close();
                        } catch (IOException e) {
                            // ignore
                        }
                    } else {
                        System.err.println("[ERROR] Cannot initialize Nodyn.");
                    }
                }
            });
        }

    }
{'fraction_non_alphanumeric': 0.08327953518398967, 'fraction_numerical': 0.0, 'mean_word_length': 2.406816932380429, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4891515', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1452, 'n_words': 334}