зеркало из https://github.com/mozilla/gecko-dev.git
added ContextFactory which is used to create Context instances in JavaAdapter when there is no Context associated with the current thread.
This commit is contained in:
Родитель
c3ac073e09
Коммит
172c10e986
|
@ -349,6 +349,15 @@ public class Context
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return {@link ContextFactory} instance to use to create Context instances
|
||||||
|
* for other execution threads.
|
||||||
|
*/
|
||||||
|
protected ContextFactory factory()
|
||||||
|
{
|
||||||
|
return ContextFactory.getDefault();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call {@link
|
* Call {@link
|
||||||
* Callable#call(Context cx, Scriptable scope, Scriptable thisObj,
|
* Callable#call(Context cx, Scriptable scope, Scriptable thisObj,
|
||||||
|
@ -364,6 +373,14 @@ public class Context
|
||||||
public static Object call(Callable callable, Scriptable scope,
|
public static Object call(Callable callable, Scriptable scope,
|
||||||
Scriptable thisObj, Object[] args)
|
Scriptable thisObj, Object[] args)
|
||||||
throws JavaScriptException
|
throws JavaScriptException
|
||||||
|
{
|
||||||
|
return call(ContextFactory.getDefault(), callable, scope, thisObj,
|
||||||
|
args);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Object call(ContextFactory f, Callable callable, Scriptable scope,
|
||||||
|
Scriptable thisObj, Object[] args)
|
||||||
|
throws JavaScriptException
|
||||||
{
|
{
|
||||||
Context[] storage = getThreadContextStorage();
|
Context[] storage = getThreadContextStorage();
|
||||||
Context cx;
|
Context cx;
|
||||||
|
@ -377,7 +394,7 @@ public class Context
|
||||||
return callable.call(cx, scope, thisObj, args);
|
return callable.call(cx, scope, thisObj, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
cx = new Context();
|
cx = f.newContext();
|
||||||
if (!cx.creationEventWasSent) {
|
if (!cx.creationEventWasSent) {
|
||||||
cx.creationEventWasSent = true;
|
cx.creationEventWasSent = true;
|
||||||
cx.runListeners(CONTEXT_CREATED_EVENT);
|
cx.runListeners(CONTEXT_CREATED_EVENT);
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||||
|
|
||||||
|
/* ***** BEGIN LICENSE BLOCK *****
|
||||||
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||||
|
*
|
||||||
|
* The contents of this file are subject to the Mozilla Public License Version
|
||||||
|
* 1.1 (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.mozilla.org/MPL/
|
||||||
|
*
|
||||||
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||||
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||||
|
* for the specific language governing rights and limitations under the
|
||||||
|
* License.
|
||||||
|
*
|
||||||
|
* The Original Code is a factory class for Context creation.
|
||||||
|
*
|
||||||
|
* The Initial Developer of the Original Code is
|
||||||
|
* RUnit Software AS.
|
||||||
|
* Portions created by the Initial Developer are Copyright (C) 2004
|
||||||
|
* the Initial Developer. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Contributor(s): Igor Bukanov, igor@fastmail.fm
|
||||||
|
*
|
||||||
|
* Alternatively, the contents of this file may be used under the terms of
|
||||||
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||||
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||||
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||||
|
* of those above. If you wish to allow use of your version of this file only
|
||||||
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||||
|
* use your version of this file under the terms of the MPL, indicate your
|
||||||
|
* decision by deleting the provisions above and replace them with the notice
|
||||||
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||||
|
* the provisions above, a recipient may use your version of this file under
|
||||||
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||||
|
*
|
||||||
|
* ***** END LICENSE BLOCK ***** */
|
||||||
|
|
||||||
|
// API class
|
||||||
|
|
||||||
|
package org.mozilla.javascript;
|
||||||
|
|
||||||
|
|
||||||
|
public class ContextFactory
|
||||||
|
{
|
||||||
|
protected Context newContext()
|
||||||
|
{
|
||||||
|
return new Context();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final Object call(Callable callable, Scriptable scope,
|
||||||
|
Scriptable thisObj, Object[] args)
|
||||||
|
{
|
||||||
|
return Context.call(this, callable, scope, thisObj, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ContextFactory getDefault()
|
||||||
|
{
|
||||||
|
return defaultFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ContextFactory defaultFactory = new ContextFactory();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -69,6 +69,7 @@ public final class JavaAdapter
|
||||||
copy = null;
|
copy = null;
|
||||||
}
|
}
|
||||||
copy.function = function;
|
copy.function = function;
|
||||||
|
copy.contextFactory = currentFactory();
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +85,7 @@ public final class JavaAdapter
|
||||||
{
|
{
|
||||||
Scriptable scope = function.getParentScope();
|
Scriptable scope = function.getParentScope();
|
||||||
Scriptable thisObj = scope;
|
Scriptable thisObj = scope;
|
||||||
return Context.call(this, scope, thisObj, args);
|
return contextFactory.call(this, scope, thisObj, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
|
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
|
||||||
|
@ -106,6 +107,7 @@ public final class JavaAdapter
|
||||||
|
|
||||||
|
|
||||||
private Function function;
|
private Function function;
|
||||||
|
private ContextFactory contextFactory;
|
||||||
private int[] argsToConvert;
|
private int[] argsToConvert;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -380,6 +382,10 @@ public final class JavaAdapter
|
||||||
cfw.addField("self", "Lorg/mozilla/javascript/Scriptable;",
|
cfw.addField("self", "Lorg/mozilla/javascript/Scriptable;",
|
||||||
(short) (ClassFileWriter.ACC_PUBLIC |
|
(short) (ClassFileWriter.ACC_PUBLIC |
|
||||||
ClassFileWriter.ACC_FINAL));
|
ClassFileWriter.ACC_FINAL));
|
||||||
|
cfw.addField("contextFactory",
|
||||||
|
"Lorg/mozilla/javascript/ContextFactory;",
|
||||||
|
(short) (ClassFileWriter.ACC_PRIVATE |
|
||||||
|
ClassFileWriter.ACC_FINAL));
|
||||||
int interfacesCount = interfaces == null ? 0 : interfaces.length;
|
int interfacesCount = interfaces == null ? 0 : interfaces.length;
|
||||||
for (int i=0; i < interfacesCount; i++) {
|
for (int i=0; i < interfacesCount; i++) {
|
||||||
if (interfaces[i] != null)
|
if (interfaces[i] != null)
|
||||||
|
@ -596,12 +602,18 @@ public final class JavaAdapter
|
||||||
return (Function)x;
|
return (Function)x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ContextFactory currentFactory()
|
||||||
|
{
|
||||||
|
return Context.getContext().factory();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility method which dynamically binds a Context to the current thread,
|
* Utility method which dynamically binds a Context to the current thread,
|
||||||
* if none already exists.
|
* if none already exists.
|
||||||
*/
|
*/
|
||||||
public static Object callMethod(Scriptable scope, Scriptable thisObj,
|
public static Object callMethod(Scriptable scope, Scriptable thisObj,
|
||||||
Function f, Object[] args, long argsToWrap)
|
Function f, Object[] args, long argsToWrap,
|
||||||
|
ContextFactory factory)
|
||||||
{
|
{
|
||||||
if (f == null) {
|
if (f == null) {
|
||||||
// See comments in getFunction
|
// See comments in getFunction
|
||||||
|
@ -612,7 +624,7 @@ public final class JavaAdapter
|
||||||
if (cx != null) {
|
if (cx != null) {
|
||||||
return doCall(cx, scope, thisObj, f, args, argsToWrap);
|
return doCall(cx, scope, thisObj, f, args, argsToWrap);
|
||||||
} else {
|
} else {
|
||||||
cx = Context.enter();
|
cx = Context.enter(factory.newContext());
|
||||||
try {
|
try {
|
||||||
return doCall(cx, scope, thisObj, f, args, argsToWrap);
|
return doCall(cx, scope, thisObj, f, args, argsToWrap);
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -668,6 +680,8 @@ public final class JavaAdapter
|
||||||
cfw.add(ByteCode.PUTFIELD, adapterName, "self",
|
cfw.add(ByteCode.PUTFIELD, adapterName, "self",
|
||||||
"Lorg/mozilla/javascript/Scriptable;");
|
"Lorg/mozilla/javascript/Scriptable;");
|
||||||
|
|
||||||
|
generateContextFactoryInit(cfw, adapterName);
|
||||||
|
|
||||||
cfw.add(ByteCode.RETURN);
|
cfw.add(ByteCode.RETURN);
|
||||||
cfw.stopMethod((short)3); // 2: this + delegee
|
cfw.stopMethod((short)3); // 2: this + delegee
|
||||||
}
|
}
|
||||||
|
@ -698,6 +712,8 @@ public final class JavaAdapter
|
||||||
cfw.add(ByteCode.PUTFIELD, adapterName, "self",
|
cfw.add(ByteCode.PUTFIELD, adapterName, "self",
|
||||||
"Lorg/mozilla/javascript/Scriptable;");
|
"Lorg/mozilla/javascript/Scriptable;");
|
||||||
|
|
||||||
|
generateContextFactoryInit(cfw, adapterName);
|
||||||
|
|
||||||
cfw.add(ByteCode.RETURN);
|
cfw.add(ByteCode.RETURN);
|
||||||
cfw.stopMethod((short)20); // TODO: magic number "20"
|
cfw.stopMethod((short)20); // TODO: magic number "20"
|
||||||
}
|
}
|
||||||
|
@ -745,10 +761,24 @@ public final class JavaAdapter
|
||||||
cfw.add(ByteCode.PUTFIELD, adapterName, "self",
|
cfw.add(ByteCode.PUTFIELD, adapterName, "self",
|
||||||
"Lorg/mozilla/javascript/Scriptable;");
|
"Lorg/mozilla/javascript/Scriptable;");
|
||||||
|
|
||||||
|
generateContextFactoryInit(cfw, adapterName);
|
||||||
|
|
||||||
cfw.add(ByteCode.RETURN);
|
cfw.add(ByteCode.RETURN);
|
||||||
cfw.stopMethod((short)2); // this + delegee
|
cfw.stopMethod((short)2); // this + delegee
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void generateContextFactoryInit(ClassFileWriter cfw,
|
||||||
|
String adapterName)
|
||||||
|
{
|
||||||
|
cfw.add(ByteCode.ALOAD_0); // this for the following PUTFIELD for self
|
||||||
|
cfw.addInvoke(ByteCode.INVOKESTATIC,
|
||||||
|
"org/mozilla/javascript/JavaAdapter",
|
||||||
|
"currentFactory",
|
||||||
|
"()Lorg/mozilla/javascript/ContextFactory;");
|
||||||
|
cfw.add(ByteCode.PUTFIELD, adapterName, "contextFactory",
|
||||||
|
"Lorg/mozilla/javascript/ContextFactory;");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates code to wrap Java arguments into Object[].
|
* Generates code to wrap Java arguments into Object[].
|
||||||
* Non-primitive Java types are left as is pending convertion
|
* Non-primitive Java types are left as is pending convertion
|
||||||
|
@ -972,6 +1002,10 @@ public final class JavaAdapter
|
||||||
}
|
}
|
||||||
cfw.addPush(convertionMask);
|
cfw.addPush(convertionMask);
|
||||||
|
|
||||||
|
cfw.add(ByteCode.ALOAD_0);
|
||||||
|
cfw.add(ByteCode.GETFIELD, genName, "contextFactory",
|
||||||
|
"Lorg/mozilla/javascript/ContextFactory;");
|
||||||
|
|
||||||
// go through utility method, which creates a Context to run the
|
// go through utility method, which creates a Context to run the
|
||||||
// method in.
|
// method in.
|
||||||
cfw.addInvoke(ByteCode.INVOKESTATIC,
|
cfw.addInvoke(ByteCode.INVOKESTATIC,
|
||||||
|
@ -982,6 +1016,7 @@ public final class JavaAdapter
|
||||||
+"Lorg/mozilla/javascript/Function;"
|
+"Lorg/mozilla/javascript/Function;"
|
||||||
+"[Ljava/lang/Object;"
|
+"[Ljava/lang/Object;"
|
||||||
+"J"
|
+"J"
|
||||||
|
+"Lorg/mozilla/javascript/ContextFactory;"
|
||||||
+")Ljava/lang/Object;");
|
+")Ljava/lang/Object;");
|
||||||
|
|
||||||
generateReturnResult(cfw, returnType);
|
generateReturnResult(cfw, returnType);
|
||||||
|
|
Загрузка…
Ссылка в новой задаче