[gjs/wip/require: 1/7] context: Move the majority of gjs_context_eval to jsapi-util
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/require: 1/7] context: Move the majority of gjs_context_eval to jsapi-util
- Date: Fri, 3 Jan 2014 16:47:12 +0000 (UTC)
commit b088f1ad235f03d40e90a2642cdf31b26b1e0511
Author: Jasper St. Pierre <jstpierre mecheye net>
Date: Thu Jan 2 15:01:41 2014 -0500
context: Move the majority of gjs_context_eval to jsapi-util
And use it in the importer as well...
gjs/byteArray.cpp | 11 ++----
gjs/context.cpp | 94 +++++++---------------------------------------------
gjs/importer.cpp | 31 +----------------
gjs/jsapi-util.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++
gjs/jsapi-util.h | 8 ++++
5 files changed, 117 insertions(+), 119 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 4c5113b..07f0550 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -770,14 +770,9 @@ byte_array_ensure_initialized (JSContext *context)
static gsize initialized = 0;
if (g_once_init_enter (&initialized)) {
- jsval rval;
- JS::CompileOptions options(context);
- options.setUTF8(true)
- .setFileAndLine("<internal>", 1);
- js::RootedObject rootedObj(context, JS_GetGlobalObject(context));
- JS::Evaluate(context, rootedObj, options,
- "imports.byteArray.ByteArray;", 27,
- &rval);
+ gjs_eval_with_scope(context, NULL,
+ "imports.byteArray.ByteArray;", 27,
+ "<internal>", NULL, NULL);
g_once_init_leave (&initialized, 1);
}
}
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 781e0be..93ef2a4 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -822,101 +822,31 @@ gjs_context_get_native_context (GjsContext *js_context)
}
gboolean
-gjs_context_eval(GjsContext *js_context,
+gjs_context_eval(GjsContext *js_context,
const char *script,
gssize script_len,
const char *filename,
int *exit_status_p,
GError **error)
{
- int line_number;
+ gboolean ret = FALSE;
jsval retval;
- gboolean success;
g_object_ref(G_OBJECT(js_context));
- if (exit_status_p)
- *exit_status_p = 1; /* "Failure" (like a shell script) */
-
- /* whether we evaluated the script OK; not related to whether
- * script returned nonzero. We set GError if success = FALSE
- */
- success = TRUE;
-
- /* handle scripts with UNIX shebangs */
- line_number = 1;
- if (script != NULL && script[0] == '#' && script[1] == '!') {
- const char *s;
-
- s = (const char *) strstr (script, "\n");
- if (s != NULL) {
- if (script_len > 0)
- script_len -= (s + 1 - script);
- script = s + 1;
- line_number = 2;
- }
- }
-
- /* log and clear exception if it's set (should not be, normally...) */
- if (gjs_log_exception(js_context->context)) {
- gjs_debug(GJS_DEBUG_CONTEXT,
- "Exception was set prior to JS_EvaluateScript()");
- }
-
- /* JS_EvaluateScript requires a request even though it sort of seems like
- * it means we're always in a request?
- */
- JS_BeginRequest(js_context->context);
-
- retval = JSVAL_VOID;
- if (script_len < 0)
- script_len = strlen(script);
-
- JSAutoCompartment ac(js_context->context, js_context->global);
- JS::CompileOptions options(js_context->context);
- options.setUTF8(true)
- .setFileAndLine(filename, line_number)
- .setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
- js::RootedObject rootedObj(js_context->context, js_context->global);
-
- if (!JS::Evaluate(js_context->context,
- rootedObj,
- options,
- script,
- script_len,
- &retval)) {
-
- gjs_debug(GJS_DEBUG_CONTEXT,
- "Script evaluation failed");
-
- gjs_log_exception(js_context->context);
- g_set_error(error,
- GJS_ERROR,
- GJS_ERROR_FAILED,
- "JS_EvaluateScript() failed");
-
- success = FALSE;
- }
-
- gjs_debug(GJS_DEBUG_CONTEXT,
- "Script evaluation succeeded");
-
- if (gjs_log_exception(js_context->context)) {
- g_set_error(error,
- GJS_ERROR,
- GJS_ERROR_FAILED,
- "Exception was set even though JS_EvaluateScript() returned true - did you gjs_throw()
but not return false somewhere perhaps?");
- success = FALSE;
- }
+ if (!gjs_eval_with_scope(js_context->context,
+ js_context->global,
+ script, script_len, filename,
+ &retval, error))
+ goto out;
- if (success && exit_status_p) {
+ if (exit_status_p) {
if (JSVAL_IS_INT(retval)) {
int code;
if (JS_ValueToInt32(js_context->context, retval, &code)) {
gjs_debug(GJS_DEBUG_CONTEXT,
"Script returned integer code %d", code);
-
*exit_status_p = code;
}
} else {
@@ -925,15 +855,15 @@ gjs_context_eval(GjsContext *js_context,
}
}
- JS_EndRequest(js_context->context);
+ ret = TRUE;
+ out:
g_object_unref(G_OBJECT(js_context));
-
- return success;
+ return ret;
}
gboolean
-gjs_context_eval_file(GjsContext *js_context,
+gjs_context_eval_file(GjsContext *js_context,
const char *filename,
int *exit_status_p,
GError **error)
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index f1f7a7b..00bea71 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -270,7 +270,6 @@ import_file(JSContext *context,
GError *error = NULL;
JS::CompileOptions options(context);
- js::RootedObject rootedObj(context, module_obj);
if (!(g_file_load_contents(file, NULL, &script, &script_len, NULL, &error))) {
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY) &&
@@ -287,35 +286,9 @@ import_file(JSContext *context,
full_path = g_file_get_parse_name (file);
-
- options.setUTF8(true)
- .setFileAndLine(full_path, 1)
- .setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
-
- if (!JS::Evaluate(context,
- rootedObj,
- options,
- script,
- script_len,
- &script_retval)) {
-
-
- /* If JSOPTION_DONT_REPORT_UNCAUGHT is set then the exception
- * would be left set after the evaluate and not go to the error
- * reporter function.
- */
- if (JS_IsExceptionPending(context)) {
- gjs_debug(GJS_DEBUG_IMPORTER,
- "Module '%s' left an exception set",
- name);
- gjs_log_and_keep_exception(context);
- } else {
- gjs_throw(context,
- "JS_EvaluateScript() returned FALSE but did not set exception");
- }
-
+ if (!gjs_eval_with_scope(context, module_obj, script, script_len,
+ full_path, NULL, NULL))
goto out;
- }
ret = JS_TRUE;
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index de39a5f..59b3dc1 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -27,6 +27,7 @@
#include <util/log.h>
#include <util/glib.h>
#include <util/misc.h>
+#include <util/error.h>
#include "jsapi-util.h"
#include "compat.h"
@@ -1165,3 +1166,94 @@ gjs_unblock_gc(void)
{
g_mutex_unlock(&gc_lock);
}
+
+static void
+strip_unix_shebang(const char **script,
+ gssize *script_len,
+ int *line_number)
+{
+ /* handle scripts with UNIX shebangs */
+ if (strncmp(*script, "#!", *script_len) == 0) {
+ const char *s;
+
+ s = (const char *) strstr (*script, "\n");
+ if (s != NULL) {
+ if (*script_len > 0)
+ *script_len -= (s + 1 - *script);
+ *script = s + 1;
+ *line_number++;
+ }
+ }
+}
+
+JSBool
+gjs_eval_with_scope(JSContext *context,
+ JSObject *object,
+ const char *script,
+ gssize script_len,
+ const char *filename,
+ jsval *retval_p,
+ GError **error)
+{
+ JSBool ret = JS_FALSE;
+ int line_number = 1;
+ jsval retval = JSVAL_VOID;
+
+ if (script_len < 0)
+ script_len = strlen(script);
+ strip_unix_shebang(&script, &script_len, &line_number);
+
+ /* log and clear exception if it's set (should not be, normally...) */
+ if (gjs_log_exception(context)) {
+ gjs_debug(GJS_DEBUG_CONTEXT,
+ "Exception was set prior to JS_EvaluateScript()");
+ }
+
+ /* JS_EvaluateScript requires a request even though it sort of seems like
+ * it means we're always in a request?
+ */
+ JS_BeginRequest(context);
+
+ if (!object)
+ object = JS_GetGlobalObject(context);
+
+ JSAutoCompartment ac(context, object);
+
+ JS::CompileOptions options(context);
+ options.setUTF8(true)
+ .setFileAndLine(filename, line_number)
+ .setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
+
+ js::RootedObject rootedObj(context, object);
+
+ if (!JS::Evaluate(context, rootedObj, options, script, script_len, &retval)) {
+ gjs_debug(GJS_DEBUG_CONTEXT,
+ "Script evaluation failed");
+
+ gjs_log_exception(context);
+ g_set_error(error,
+ GJS_ERROR,
+ GJS_ERROR_FAILED,
+ "JS_EvaluateScript() failed");
+ goto out;
+ }
+
+ gjs_debug(GJS_DEBUG_CONTEXT,
+ "Script evaluation succeeded");
+
+ if (gjs_log_exception(context)) {
+ g_set_error(error,
+ GJS_ERROR,
+ GJS_ERROR_FAILED,
+ "Exception was set even though JS_EvaluateScript() returned true - did you gjs_throw()
but not return false somewhere perhaps?");
+ goto out;
+ }
+
+ ret = JS_TRUE;
+ if (retval_p)
+ *retval_p = retval;
+
+ out:
+ JS_EndRequest(context);
+ return ret;
+}
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index a828873..0b69ac5 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -383,6 +383,14 @@ JSBool gjs_context_get_frame_info (JSContext *context,
jsval *fileName,
jsval *lineNumber);
+JSBool gjs_eval_with_scope (JSContext *context,
+ JSObject *object,
+ const char *script,
+ gssize script_len,
+ const char *filename,
+ jsval *retval_p,
+ GError **error);
+
G_END_DECLS
#endif /* __GJS_JSAPI_UTIL_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]