-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhack.js
82 lines (64 loc) · 3.09 KB
/
hack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var print = function print(_message)
{
Packages.java.lang.System.out.println(_message);
};
var createContext = function createContext()
{
//Do not try to manage the context ... as it is managed by
//Rhino already. Trying to check to see is the current context
//is used and using that instead will circumvent Rhino context
//context counting mechanism. This could lead to strange issues
//popping up like disappearing object references and staying up
//late at night :) ... Bediako 06/18/2011 5:49 am.
var context = (new Packages.org.mozilla.javascript.ContextFactory()).enterContext();
context.setLanguageVersion(180);
context.setOptimizationLevel(-1); //in app engine it turns out that interpreted mode is fastest for handler execution.
return context;
};
var resetScope = function resetScope(_context, _sharedScope)
{
var scriptable = _context.newObject(_sharedScope);
scriptable.setPrototype(_sharedScope);
scriptable.setParentScope(null);
return scriptable;
};
var context = createContext();
var sharedScope = context.initStandardObjects();
context.evaluateString(sharedScope, Packages.airlift.util.JavaScriptingUtil.shim, "shim", 1, null);
var uris = new Packages.java.util.HashSet();
importClass(Packages.java.net.URI);
//Note that we are really not calling out to the
//http ... we ultimately just use the path of this
//URI to find the resource in the jar.
uris.add(new URI("http://localhost:80/airlift"));
uris.add(new URI("http://localhost:80//airlift/lib/"));
uris.add(new java.net.URI("http://localhost:80/handler/"));
uris.add(new java.net.URI("http://localhost:80/extlib/"));
uris.add(new java.net.URI("http://localhost:80/gen/"));
uris.add(new URI("http://localhost:80//node_modules/"));
uris.add(new URI("http://localhost:80//"));
importClass(Packages.airlift.util.SharedRequire);
importClass(Packages.airlift.util.Require);
importClass(Packages.org.mozilla.javascript.commonjs.module.provider.StrongCachingModuleScriptProvider);
importClass(Packages.airlift.util.UrlModuleSourceProvider);
var sharedRequire = new SharedRequire(
context, sharedScope, new StrongCachingModuleScriptProvider(
new UrlModuleSourceProvider(null, uris)), null, null, false);
var testDir = project.getProperty("src.test");
var script = project.getProperty("script");
var directoryScanner = new org.apache.tools.ant.DirectoryScanner();
var includes = Packages.java.lang.reflect.Array.newInstance(Packages.java.lang.String, 1);
includes[0] = script + ".js";
var baseDir = project.getBaseDir().getPath() + "/src/script/javascript/";
directoryScanner.setIncludes(includes);
directoryScanner.setBasedir(new Packages.java.io.File(baseDir));
directoryScanner.setCaseSensitive(true);
directoryScanner.scan();
print("Executing script ...");
var files = directoryScanner.getIncludedFiles();
var scriptName = files[0];
var hack = "var name = \"" + script + "\"; var script = require(name);";
var scope = resetScope(context, sharedScope);
var require = new Packages.airlift.util.Require(scope, sharedRequire, new Packages.java.util.HashMap(), true);
require.install(scope);
context.evaluateString(scope, hack, scriptName, 1, null);