This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathJavaScriptModule.cs
More file actions
157 lines (139 loc) · 6.37 KB
/
Copy pathJavaScriptModule.cs
File metadata and controls
157 lines (139 loc) · 6.37 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2014 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Jurassic;
using Jurassic.Compiler;
using Jurassic.Library;
using System;
using System.Collections.Generic;
using System.IO;
namespace SharpJS
{
class JavaScriptModule : Module
{
private string Code;
private string Path;
public ScriptSource Source = null;
private FunctionInstance Require;
private static string Wrap(string code) {
// For now we have to force strict mode so that a JS module cannot set any properties
// on the one and only global scope.
return "(function(module, exports, require, __filename, __dirname) { \"use strict\"; " + code + "})";
}
public JavaScriptModule(Context context, string code, string path) : base(context) {
Code = code;
Path = path;
Initialize();
}
public JavaScriptModule(Context context, string path) : base(context) {
Path = context.Path.Resolve(path);
StreamReader sr = File.OpenText(path);
try {
Code = sr.ReadToEnd();
} finally {
sr.Close();
}
Initialize();
}
private void Initialize() {
Require = new RequireFunction(this);
}
private void Validate(string code) {
// When wrapping the code for CommonJS, we have to make sure that the code we wrap
// is valid so that it cannot inject other functions on the top level scope, like
// if it'd contain "}); (function() {". So we have to validate that before wrapping:
Engine.TryParse(new StringScriptSource(code, Path)); // throws SyntaxError
}
public class RequireFunction : FunctionInstance
{
public JavaScriptModule Module;
public ObjectInstance Modules;
public RequireFunction(JavaScriptModule module) : base(module.Context.Engine.Function.InstancePrototype) {
Module = module;
Modules = Module.Engine.Object.Construct();
SetPropertyValue("cache", Modules, false);
}
public override object CallLateBound(object thisObject, params object[] args) {
if (args.Length == 0)
throw new JavaScriptException(Module.Context.Engine, "TypeError", "null");
if (!(args[0] is string))
throw new JavaScriptException(Module.Context.Engine, "TypeError", "illegal module name");
string filename = args[0] as string;
// Lookup global module, like "fs"
object exports = Module.Context.TryRequire(filename);
if (exports != null)
return exports;
// Lookup local module
if (Modules.HasProperty(filename))
return Modules[filename];
// Else try to load a local JavaScript module
string dirname = Module.Context.Path.Dirname(Module.Source.Path);
filename = Module.Context.Fs.CheckPath(Module.Context.Path.Join(dirname, filename));
try {
if (!File.Exists(filename))
filename = filename + ".js";
StreamReader sr = File.OpenText(filename);
try {
string code = sr.ReadToEnd();
JavaScriptModule module = new JavaScriptModule(Module.Context, code, filename);
Modules.SetPropertyValue(filename, exports = module.Run(), true);
return exports;
} finally {
sr.Close();
}
} catch (Exception ex) {
throw new JavaScriptException(Module.Context.Engine, "Error", ex.Message);
}
}
}
public override object Run() {
if (Source == null) {
Validate(Code);
Source = new StringScriptSource(Wrap(Code), Path);
Code = null;
Path = null;
}
FunctionInstance wrap = (FunctionInstance)Engine.Evaluate(Source);
ObjectInstance module = Engine.Object.Construct();
ObjectInstance exports = Engine.Object.Construct();
module.SetPropertyValue("exports", exports, true);
string filename = System.IO.Path.GetFullPath(Source.Path);
string dirname = System.IO.Path.GetDirectoryName(filename);
wrap.CallLateBound(this, new object[] { module, exports, Require, filename, dirname });
return module.GetPropertyValue("exports");
}
/* public override object Run() {
// Switching the global context here requires some monkey patches to
// Jurassic to a) access the GlobalObject constructor, b) assign
// a replacement global object to the engine and c) recreate the
// initial global namespace on the new GlobalObject.
GlobalObject savedGlobal = Engine.Global;
Engine.Global = new GlobalObject(Engine.Object.InstancePrototype); // a), b)
Engine.PopulateGlobals(); // c)
// Register SharpJS globals
Context.RegisterGlobals();
// Register module global
ObjectInstance module = Engine.Object.Construct(),
exports = Engine.Object.Construct();
Engine.SetGlobalValue("module", module);
module.SetPropertyValue("exports", exports, false);
Engine.SetGlobalValue("exports", exports);
// And run it
try {
Engine.Evaluate(Source);
return module.GetPropertyValue("exports");
} finally {
Engine.Global = savedGlobal;
}
} */
}
}