Skip to content

Instantly share code, notes, and snippets.

@kaosat-dev
Created September 5, 2013 12:01
Show Gist options
  • Save kaosat-dev/6449222 to your computer and use it in GitHub Desktop.
Save kaosat-dev/6449222 to your computer and use it in GitHub Desktop.
#see https://github.com/joyent/node/blob/master/src/node.js#L367
#and http://stackoverflow.com/questions/9475792/how-does-require-in-node-js-work
#and https://github.com/joyent/node/blob/906a175a0b40fc56dfafbe0929e4fbbb4df68ff2/src/node.js#L699
globals = {}
class Module
constructor:->
@exports = {}
@content = ""
compile:()->
#f = new Function( wrapper )
#toto = f(globals, module, assembly)
#f = new Function(["module","assembly"], wrapper )
#toto = f.apply(null, [module,assembly])
wrap:(script)->
wrapped = """
return (function ( exports, include, module, , __filename, __dirname)
{
console.log("exports",exports, "module",module);
//#{script}
});
"""
return wrapped
class Assembly
constructor:->
@contents = []
add:(item)=>
@contents.push( item )
module = new Module()
assembly = new Assembly()
script = """
//assembly.add( "toto")
module.exports = 24
"""
wrapper = """
return (function ( exports, include, module, , __filename, __dirname)
{
console.log("assembly",assembly, "module",module);
//#{script}
//console.log(assembly);
});
"""
#console.log toto
#toto(module, assembly)
#console.log wrapper
#console.log module
#console.log assembly
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[add your bin description]" />
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment