runInContext -> evalModule
This commit is contained in:
parent
616749377b
commit
14b8b76a79
|
@ -67,8 +67,8 @@ const compiler = {
|
||||||
trace: false
|
trace: false
|
||||||
} );
|
} );
|
||||||
|
|
||||||
// We want `session.vm.runInContext` to return the parser
|
// We want `session.vm.evalModule` to return the parser
|
||||||
if ( options.output === "parser" ) options.format = "bare";
|
if ( options.output === "parser" ) options.format = "umd";
|
||||||
|
|
||||||
util.each( session.passes, stage => {
|
util.each( session.passes, stage => {
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ const compiler = {
|
||||||
switch ( options.output ) {
|
switch ( options.output ) {
|
||||||
|
|
||||||
case "parser":
|
case "parser":
|
||||||
return session.vm.runInContext( ast.code, options.context );
|
return session.vm.evalModule( ast.code, options.context );
|
||||||
|
|
||||||
case "source":
|
case "source":
|
||||||
return ast.code;
|
return ast.code;
|
||||||
|
|
|
@ -7,6 +7,7 @@ const GrammarError = require( "../grammar-error" );
|
||||||
const opcodes = require( "./opcodes" );
|
const opcodes = require( "./opcodes" );
|
||||||
const parser = require( "../parser" );
|
const parser = require( "../parser" );
|
||||||
const util = require( "../util" );
|
const util = require( "../util" );
|
||||||
|
const vm = require( "../util/vm" );
|
||||||
|
|
||||||
function fatal( message, location ) {
|
function fatal( message, location ) {
|
||||||
|
|
||||||
|
@ -28,9 +29,7 @@ class Session {
|
||||||
this.parser = config.parser || parser;
|
this.parser = config.parser || parser;
|
||||||
this.passes = config.passes || {};
|
this.passes = config.passes || {};
|
||||||
this.visitor = config.visitor || ast.visitor;
|
this.visitor = config.visitor || ast.visitor;
|
||||||
this.vm = config.vm || {
|
this.vm = config.vm || vm;
|
||||||
runInContext: util.runInContext
|
|
||||||
};
|
|
||||||
|
|
||||||
if ( typeof config.warn === "function" ) this.warn = config.warn;
|
if ( typeof config.warn === "function" ) this.warn = config.warn;
|
||||||
if ( typeof config.error === "function" ) this.error = config.error;
|
if ( typeof config.error === "function" ) this.error = config.error;
|
||||||
|
|
|
@ -1,26 +1,25 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `eval` the given source, using properties found in `context` as top-level variables.
|
* `eval` the given source as a CommonJS module, using properties found in `context` as top-level variables.
|
||||||
*
|
*
|
||||||
* Based on `vm.runInContext` found in Node.js, this is a cross-env solution.
|
* Based on `vm.runInContext` found in Node.js, this is a cross-env solution.
|
||||||
*/
|
*/
|
||||||
function runInContext( source, context ) {
|
function evalModule( source, context ) {
|
||||||
|
|
||||||
const argumentKeys = Object.keys( context );
|
const argumentKeys = Object.keys( context );
|
||||||
const argumentValues = argumentKeys.map( argument => context[ argument ] );
|
const argumentValues = argumentKeys.map( argument => context[ argument ] );
|
||||||
|
|
||||||
const object = {};
|
const sandbox = { exports: {} };
|
||||||
argumentKeys.push( "_peg$object", `_peg$object.result = ${ source };` );
|
argumentKeys.push( "module", "exports", source );
|
||||||
argumentValues.push( object );
|
argumentValues.push( sandbox, sandbox.exports );
|
||||||
|
|
||||||
Function( ...argumentKeys )( ...argumentValues );
|
Function( ...argumentKeys )( ...argumentValues );
|
||||||
|
|
||||||
return object.result;
|
return sandbox.exports;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exports
|
// Exports
|
||||||
|
|
||||||
module.exports = { runInContext };
|
module.exports = { evalModule };
|
||||||
|
|
2
packages/pegjs/typings/api.d.ts
vendored
2
packages/pegjs/typings/api.d.ts
vendored
|
@ -435,7 +435,7 @@ declare namespace peg {
|
||||||
|
|
||||||
interface ISessionVM {
|
interface ISessionVM {
|
||||||
|
|
||||||
runInContext( code: string, context?: { [ name: string ]: any; } ): any;
|
evalModule( code: string, context?: { [ name: string ]: any; } ): any;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -131,7 +131,7 @@ describe( "plugin API", function () {
|
||||||
|
|
||||||
function pass( ast ) {
|
function pass( ast ) {
|
||||||
|
|
||||||
ast.code = "({ parse: function() { return 42; } })";
|
ast.code = "exports.parse = function() { return 42; }";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue