forked from TheBrokenRail/dukluv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype Duktape 2.0 require system polyfill
- Loading branch information
1 parent
d126b3f
commit 638d8fe
Showing
5 changed files
with
164 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
return 42; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"use strict"; | ||
|
||
// Replace Duktape 1.x require with polyfilled 2.x require | ||
// This won't be needed when new require is native. | ||
var module = {id:uv.cwd()+"/test-require.js",exports:{}}; | ||
require = newRequire.bind(module); | ||
|
||
// Set up our app-specefic require hooks. | ||
Duktape.modResolve = function (parent, id) { | ||
if (id[0] !== ".") { return id; } | ||
return pathJoin(parent.id, "..", id); | ||
}; | ||
|
||
Duktape.modLoad = function (module) { | ||
var code = loadFile(module.id); | ||
return Duktape.modCompile(module, code); | ||
}; | ||
|
||
// Test require | ||
var p = require('./modules/utils.js').prettyPrint; | ||
p(require('./b.js')); | ||
|
||
require('./tcp-echo.js'); | ||
|
||
function loadFile(path) { | ||
var fd = uv.fs_open(path, "r", 420 /*0644*/); | ||
try { | ||
var stat = uv.fs_fstat(fd); | ||
var chunk = uv.fs_read(fd, stat.size, 0); | ||
return chunk; | ||
} | ||
finally { | ||
uv.fs_close(fd); | ||
} | ||
} | ||
|
||
function pathJoin() { | ||
var parts = [].join.call(arguments, "/").split("/").filter(Boolean); | ||
var skip = 0; | ||
for (var i = parts.length - 1; i >= 0; --i) { | ||
var part = parts[i]; | ||
if (part === '.') { | ||
parts.splice(i, 1); | ||
} | ||
else if (part === '..') { | ||
parts.splice(i, 1); | ||
++skip; | ||
} | ||
else if (skip) { | ||
parts.splice(i, 1); | ||
--skip; | ||
} | ||
} | ||
var path = parts.join("/"); | ||
if (arguments[0][0] === "/") { | ||
path = "/" + path; | ||
} | ||
return path; | ||
} |