| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.commonjs.module; |
| 6 |
|
| 7 |
import java.io.File; |
| 8 |
import java.net.URI; |
| 9 |
import java.net.URISyntaxException; |
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.concurrent.ConcurrentHashMap; |
| 13 |
|
| 14 |
import org.mozilla.javascript.BaseFunction; |
| 15 |
import org.mozilla.javascript.Context; |
| 16 |
import org.mozilla.javascript.Script; |
| 17 |
import org.mozilla.javascript.ScriptRuntime; |
| 18 |
import org.mozilla.javascript.Scriptable; |
| 19 |
import org.mozilla.javascript.ScriptableObject; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class Require extends BaseFunction |
| 42 |
{ |
| 43 |
private static final long serialVersionUID = 1L; |
| 44 |
|
| 45 |
private final ModuleScriptProvider moduleScriptProvider; |
| 46 |
private final Scriptable nativeScope; |
| 47 |
private final Scriptable paths; |
| 48 |
private final boolean sandboxed; |
| 49 |
private final Script preExec; |
| 50 |
private final Script postExec; |
| 51 |
private String mainModuleId = null; |
| 52 |
private Scriptable mainExports; |
| 53 |
|
| 54 |
|
| 55 |
private final Map<String, Scriptable> exportedModuleInterfaces = |
| 56 |
new ConcurrentHashMap<String, Scriptable>(); |
| 57 |
private final Object loadLock = new Object(); |
| 58 |
|
| 59 |
|
| 60 |
private static final ThreadLocal<Map<String, Scriptable>> |
| 61 |
loadingModuleInterfaces = new ThreadLocal<Map<String,Scriptable>>(); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public Require(Context cx, Scriptable nativeScope, |
| 82 |
ModuleScriptProvider moduleScriptProvider, Script preExec, |
| 83 |
Script postExec, boolean sandboxed) { |
| 84 |
this.moduleScriptProvider = moduleScriptProvider; |
| 85 |
this.nativeScope = nativeScope; |
| 86 |
this.sandboxed = sandboxed; |
| 87 |
this.preExec = preExec; |
| 88 |
this.postExec = postExec; |
| 89 |
setPrototype(ScriptableObject.getFunctionPrototype(nativeScope)); |
| 90 |
if(!sandboxed) { |
| 91 |
paths = cx.newArray(nativeScope, 0); |
| 92 |
defineReadOnlyProperty(this, "paths", paths); |
| 93 |
} |
| 94 |
else { |
| 95 |
paths = null; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public Scriptable requireMain(Context cx, String mainModuleId) { |
| 117 |
if(this.mainModuleId != null) { |
| 118 |
if (!this.mainModuleId.equals(mainModuleId)) { |
| 119 |
throw new IllegalStateException("Main module already set to " + |
| 120 |
this.mainModuleId); |
| 121 |
} |
| 122 |
return mainExports; |
| 123 |
} |
| 124 |
|
| 125 |
ModuleScript moduleScript; |
| 126 |
try { |
| 127 |
|
| 128 |
moduleScript = moduleScriptProvider.getModuleScript( |
| 129 |
cx, mainModuleId, null, null, paths); |
| 130 |
} catch (RuntimeException x) { |
| 131 |
throw x; |
| 132 |
} catch (Exception x) { |
| 133 |
throw new RuntimeException(x); |
| 134 |
} |
| 135 |
|
| 136 |
if (moduleScript != null) { |
| 137 |
mainExports = getExportedModuleInterface(cx, mainModuleId, |
| 138 |
null, null, true); |
| 139 |
} else if (!sandboxed) { |
| 140 |
|
| 141 |
URI mainUri = null; |
| 142 |
|
| 143 |
|
| 144 |
try { |
| 145 |
mainUri = new URI(mainModuleId); |
| 146 |
} catch (URISyntaxException usx) { |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
if (mainUri == null || !mainUri.isAbsolute()) { |
| 152 |
File file = new File(mainModuleId); |
| 153 |
if (!file.isFile()) { |
| 154 |
throw ScriptRuntime.throwError(cx, nativeScope, |
| 155 |
"Module \"" + mainModuleId + "\" not found."); |
| 156 |
} |
| 157 |
mainUri = file.toURI(); |
| 158 |
} |
| 159 |
mainExports = getExportedModuleInterface(cx, mainUri.toString(), |
| 160 |
mainUri, null, true); |
| 161 |
} |
| 162 |
|
| 163 |
this.mainModuleId = mainModuleId; |
| 164 |
return mainExports; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public void install(Scriptable scope) { |
| 173 |
ScriptableObject.putProperty(scope, "require", this); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public Object call(Context cx, Scriptable scope, Scriptable thisObj, |
| 178 |
Object[] args) |
| 179 |
{ |
| 180 |
if(args == null || args.length < 1) { |
| 181 |
throw ScriptRuntime.throwError(cx, scope, |
| 182 |
"require() needs one argument"); |
| 183 |
} |
| 184 |
|
| 185 |
String id = (String)Context.jsToJava(args[0], String.class); |
| 186 |
URI uri = null; |
| 187 |
URI base = null; |
| 188 |
if (id.startsWith("./") || id.startsWith("../")) { |
| 189 |
if (!(thisObj instanceof ModuleScope)) { |
| 190 |
throw ScriptRuntime.throwError(cx, scope, |
| 191 |
"Can't resolve relative module ID \"" + id + |
| 192 |
"\" when require() is used outside of a module"); |
| 193 |
} |
| 194 |
|
| 195 |
ModuleScope moduleScope = (ModuleScope) thisObj; |
| 196 |
base = moduleScope.getBase(); |
| 197 |
URI current = moduleScope.getUri(); |
| 198 |
uri = current.resolve(id); |
| 199 |
|
| 200 |
if (base == null) { |
| 201 |
|
| 202 |
|
| 203 |
id = uri.toString(); |
| 204 |
} else { |
| 205 |
|
| 206 |
id = base.relativize(current).resolve(id).toString(); |
| 207 |
if (id.charAt(0) == '.') { |
| 208 |
|
| 209 |
|
| 210 |
if (sandboxed) { |
| 211 |
throw ScriptRuntime.throwError(cx, scope, |
| 212 |
"Module \"" + id + "\" is not contained in sandbox."); |
| 213 |
} else { |
| 214 |
id = uri.toString(); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
return getExportedModuleInterface(cx, id, uri, base, false); |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public Scriptable construct(Context cx, Scriptable scope, Object[] args) { |
| 224 |
throw ScriptRuntime.throwError(cx, scope, |
| 225 |
"require() can not be invoked as a constructor"); |
| 226 |
} |
| 227 |
|
| 228 |
private Scriptable getExportedModuleInterface(Context cx, String id, |
| 229 |
URI uri, URI base, boolean isMain) |
| 230 |
{ |
| 231 |
|
| 232 |
Scriptable exports = exportedModuleInterfaces.get(id); |
| 233 |
if(exports != null) { |
| 234 |
if(isMain) { |
| 235 |
throw new IllegalStateException( |
| 236 |
"Attempt to set main module after it was loaded"); |
| 237 |
} |
| 238 |
return exports; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
Map<String, Scriptable> threadLoadingModules = |
| 243 |
loadingModuleInterfaces.get(); |
| 244 |
if(threadLoadingModules != null) { |
| 245 |
exports = threadLoadingModules.get(id); |
| 246 |
if(exports != null) { |
| 247 |
return exports; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
synchronized(loadLock) { |
| 259 |
|
| 260 |
|
| 261 |
exports = exportedModuleInterfaces.get(id); |
| 262 |
if(exports != null) { |
| 263 |
return exports; |
| 264 |
} |
| 265 |
|
| 266 |
final ModuleScript moduleScript = getModule(cx, id, uri, base); |
| 267 |
if (sandboxed && !moduleScript.isSandboxed()) { |
| 268 |
throw ScriptRuntime.throwError(cx, nativeScope, "Module \"" |
| 269 |
+ id + "\" is not contained in sandbox."); |
| 270 |
} |
| 271 |
exports = cx.newObject(nativeScope); |
| 272 |
|
| 273 |
final boolean outermostLocked = threadLoadingModules == null; |
| 274 |
if(outermostLocked) { |
| 275 |
threadLoadingModules = new HashMap<String, Scriptable>(); |
| 276 |
loadingModuleInterfaces.set(threadLoadingModules); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
threadLoadingModules.put(id, exports); |
| 287 |
try { |
| 288 |
|
| 289 |
|
| 290 |
Scriptable newExports = executeModuleScript(cx, id, exports, |
| 291 |
moduleScript, isMain); |
| 292 |
if (exports != newExports) { |
| 293 |
threadLoadingModules.put(id, newExports); |
| 294 |
exports = newExports; |
| 295 |
} |
| 296 |
} |
| 297 |
catch(RuntimeException e) { |
| 298 |
|
| 299 |
threadLoadingModules.remove(id); |
| 300 |
throw e; |
| 301 |
} |
| 302 |
finally { |
| 303 |
if(outermostLocked) { |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
exportedModuleInterfaces.putAll(threadLoadingModules); |
| 311 |
loadingModuleInterfaces.set(null); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
return exports; |
| 316 |
} |
| 317 |
|
| 318 |
private Scriptable executeModuleScript(Context cx, String id, |
| 319 |
Scriptable exports, ModuleScript moduleScript, boolean isMain) |
| 320 |
{ |
| 321 |
final ScriptableObject moduleObject = (ScriptableObject)cx.newObject( |
| 322 |
nativeScope); |
| 323 |
URI uri = moduleScript.getUri(); |
| 324 |
URI base = moduleScript.getBase(); |
| 325 |
defineReadOnlyProperty(moduleObject, "id", id); |
| 326 |
if(!sandboxed) { |
| 327 |
defineReadOnlyProperty(moduleObject, "uri", uri.toString()); |
| 328 |
} |
| 329 |
final Scriptable executionScope = new ModuleScope(nativeScope, uri, base); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
executionScope.put("exports", executionScope, exports); |
| 335 |
executionScope.put("module", executionScope, moduleObject); |
| 336 |
moduleObject.put("exports", moduleObject, exports); |
| 337 |
install(executionScope); |
| 338 |
if(isMain) { |
| 339 |
defineReadOnlyProperty(this, "main", moduleObject); |
| 340 |
} |
| 341 |
executeOptionalScript(preExec, cx, executionScope); |
| 342 |
moduleScript.getScript().exec(cx, executionScope); |
| 343 |
executeOptionalScript(postExec, cx, executionScope); |
| 344 |
return ScriptRuntime.toObject(cx, nativeScope, |
| 345 |
ScriptableObject.getProperty(moduleObject, "exports")); |
| 346 |
} |
| 347 |
|
| 348 |
private static void executeOptionalScript(Script script, Context cx, |
| 349 |
Scriptable executionScope) |
| 350 |
{ |
| 351 |
if(script != null) { |
| 352 |
script.exec(cx, executionScope); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
private static void defineReadOnlyProperty(ScriptableObject obj, |
| 357 |
String name, Object value) { |
| 358 |
ScriptableObject.putProperty(obj, name, value); |
| 359 |
obj.setAttributes(name, ScriptableObject.READONLY | |
| 360 |
ScriptableObject.PERMANENT); |
| 361 |
} |
| 362 |
|
| 363 |
private ModuleScript getModule(Context cx, String id, URI uri, URI base) { |
| 364 |
try { |
| 365 |
final ModuleScript moduleScript = |
| 366 |
moduleScriptProvider.getModuleScript(cx, id, uri, base, paths); |
| 367 |
if (moduleScript == null) { |
| 368 |
throw ScriptRuntime.throwError(cx, nativeScope, "Module \"" |
| 369 |
+ id + "\" not found."); |
| 370 |
} |
| 371 |
return moduleScript; |
| 372 |
} |
| 373 |
catch(RuntimeException e) { |
| 374 |
throw e; |
| 375 |
} |
| 376 |
catch(Exception e) { |
| 377 |
throw Context.throwAsScriptRuntimeEx(e); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
@Override |
| 382 |
public String getFunctionName() { |
| 383 |
return "require"; |
| 384 |
} |
| 385 |
|
| 386 |
@Override |
| 387 |
public int getArity() { |
| 388 |
return 1; |
| 389 |
} |
| 390 |
|
| 391 |
@Override |
| 392 |
public int getLength() { |
| 393 |
return 1; |
| 394 |
} |
| 395 |
} |