Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIMOB-7233: Android: evalJS and evalFile methods deprecated in TiContext #1538

Merged
merged 3 commits into from
Mar 5, 2012
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class KrollRuntime implements Handler.Callback
private static final int MSG_INIT = 100;
private static final int MSG_DISPOSE = 101;
private static final int MSG_RUN_MODULE = 102;
private static final int MSG_EVAL_STRING = 103;

private static final String PROPERTY_FILENAME = "filename";
private static final String PROPERTY_SOURCE = "source";
Expand Down Expand Up @@ -64,6 +65,7 @@ public String toString()

public static final int DONT_INTERCEPT = Integer.MIN_VALUE + 1;
public static final int DEFAULT_THREAD_STACK_SIZE = 16 * 1024;
public static final String SOURCE_ANONYMOUS = "<anonymous>";

public static class KrollRuntimeThread extends Thread
{
Expand Down Expand Up @@ -179,6 +181,46 @@ public void runModule(String source, String filename, KrollProxySupport activity
}
}

/**
* Equivalent to <pre>evalString(source, SOURCE_ANONYMOUS)</pre>
* @see evalString(String source, String filename)
* @return
*/
public Object evalString(String source)
{
return evalString(source, SOURCE_ANONYMOUS);
}

/**
* Evaluates a String of Javascript code, returning the result of the execution
* when this method is called on the KrollRuntime thread. If this method is called
* ony any other thread, then this function is asynchronous, and returns null.
*
* Currently, Kroll supports converting the following Javascript return types:
* <ul>
* <li>Primitives (String, Number, Boolean, etc)</li>
* <li>Javascript object literals as {@link org.appcelerator.kroll.KrollDict}</li>
* <li>Arrays</li>
* <li>Any Proxy type that extends {@link org.appcelerator.kroll.KrollProxy}</li>
* </ul>
* @param source A string containing Javascript source
* @param filename The name of the filename represented by {@link source}
* @return The Java representation of the return value of {@link source}, as long as Kroll supports the return value
*/
public Object evalString(String source, String filename)
{
if (isRuntimeThread()) {
return doEvalString(source, filename);

} else {
Message message = handler.obtainMessage(MSG_EVAL_STRING);
message.getData().putString(PROPERTY_SOURCE, source);
message.getData().putString(PROPERTY_FILENAME, filename);
message.sendToTarget();
return null;
}
}

public int getThreadStackSize(Context context)
{
if (context instanceof KrollApplication) {
Expand All @@ -191,21 +233,32 @@ public int getThreadStackSize(Context context)
public boolean handleMessage(Message msg)
{
switch (msg.what) {
case MSG_INIT:
case MSG_INIT: {
doInit();
return true;
}

case MSG_DISPOSE:
case MSG_DISPOSE: {
internalDispose();
return true;
}

case MSG_RUN_MODULE:
case MSG_RUN_MODULE: {
String source = msg.getData().getString(PROPERTY_SOURCE);
String filename = msg.getData().getString(PROPERTY_FILENAME);
KrollProxySupport activityProxy = (KrollProxySupport) msg.obj;

doRunModule(source, filename, activityProxy);
return true;
}

case MSG_EVAL_STRING: {
String source = msg.getData().getString(PROPERTY_SOURCE);
String filename = msg.getData().getString(PROPERTY_FILENAME);

doEvalString(source, filename);
return true;
}
}

return false;
Expand Down Expand Up @@ -279,6 +332,8 @@ public void setEvaluator(KrollEvaluator eval)

public abstract void doDispose();
public abstract void doRunModule(String source, String filename, KrollProxySupport activityProxy);
public abstract Object doEvalString(String source, String filename);

public abstract String getRuntimeName();
public abstract void initRuntime();
public abstract void initObject(KrollProxySupport proxy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ public void doRunModule(String source, String filename, KrollProxySupport activi
}
}

@Override
public Object doEvalString(String source, String filename)
{
Context context = enterContext();

try {
Object result = context.evaluateString(globalScope, source, filename, 1, null);
return TypeConverter.jsObjectToJavaObject(result, globalScope);

} catch (Exception e) {
if (e instanceof RhinoException) {
RhinoException re = (RhinoException) e;
Context.reportRuntimeError(re.getMessage(), re.sourceName(), re.lineNumber(), re.lineSource(),
re.columnNumber());

} else {
Context.reportError(e.getMessage());
}
return null;

} finally {
Context.exit();
}
}

@Override
public void initObject(KrollProxySupport proxy)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public void doRunModule(String source, String filename, KrollProxySupport activi
nativeRunModule(source, filename, activityProxy);
}

@Override
public Object doEvalString(String source, String filename)
{
return nativeEvalString(source, filename);
}

@Override
public void initObject(KrollProxySupport proxy)
{
Expand Down Expand Up @@ -155,6 +161,7 @@ public void addExternalModule(String libName, Class<? extends KrollExternalModul
// JNI method prototypes
private native void nativeInit(boolean useGlobalRefs, int debuggerPort, boolean DBG);
private native void nativeRunModule(String source, String filename, KrollProxySupport activityProxy);
private native Object nativeEvalString(String source, String filename);
private native void nativeProcessDebugMessages();
private native void nativeIdle();
private native void nativeDispose();
Expand Down
27 changes: 27 additions & 0 deletions android/runtime/v8/src/native/V8Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,33 @@ JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeRu
}
}

JNIEXPORT jobject JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeEvalString
(JNIEnv *env, jobject self, jstring source, jstring filename)
{
ENTER_V8(V8Runtime::globalContext);
titanium::JNIScope jniScope(env);

Handle<Value> jsSource = TypeConverter::javaStringToJsString(source);
if (jsSource.IsEmpty() || !jsSource->IsString()) {
LOGE(TAG, "Error converting Javascript string, aborting evalScript");
return NULL;
}

Handle<Value> jsFilename = TypeConverter::javaStringToJsString(filename);

TryCatch tryCatch;
Handle<Script> script = Script::Compile(jsSource->ToString(), jsFilename);
Local<Value> result = script->Run();

if (tryCatch.HasCaught()) {
V8Util::openJSErrorDialog(tryCatch);
V8Util::reportException(tryCatch, true);
return NULL;
}

return TypeConverter::jsValueToJavaObject(result);
}

JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeProcessDebugMessages(JNIEnv *env, jobject self)
{
v8::Debug::ProcessDebugMessages();
Expand Down