javascript - evaluate module in another module's context -



javascript - evaluate module in another module's context -

i know un-node/commonjs-y—forgive me. (i'm writing library of sorts , i'd library's require method work same on browser , on nodejs.)

what i'd able evaluate script in context of current module—that is, if exports.a = "100"; in module, i'd exports.a equal "100" in of code in requireing module after require.

if isn't clear, i'd happy elaborate.

this won't finish answer, help in right direction.

i've been messing node's scheme of creating modules lastly couple of days. wanted create modules invoked in exclusively fresh context , variable scope, define limited subset , extension of node's capabilities.

i ended studying their source here, , giving particular attending nativemodule constructor , methods.

you'll notice source of module read file, wrapped in string representing function , eval'd actual code.

the wrapper:

nativemodule.wrapper = [ '(function (exports, require, module, __filename, __dirname, define) { ', '\n});' ];

the function invoked, invokes contained module code.

the function requires half dozen arguments can see wrapper, first of exports object (which starts off empty). it's passed require function, why can access require variable though require isn't global.

the module code populates exports object , exports cached work doesn't need done in future. when require( 'somemodule' ) invoked, looks cached exports object , returns it.

i'm sure in code long can source module want require.

perhaps somemodule.tostring() sufficient you. not sure how consistent browser back upwards though.

there's private api that's used set environment modules.

process.binding('evals').script /* { [function: script] createcontext: [function], runincontext: [function], runinthiscontext: [function], runinnewcontext: [function] } */

i ended needing utilize createcontext , runincontext things working, i'd guess won't need this.

javascript node.js

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -