[gjs/wip/imports-rewrite: 2/5] Add a bootstrap system, allowing us to write JS code at early boot



commit 30976a82cd2e17789051a90649df85141c5ce092
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Thu Jan 2 14:35:54 2014 -0500

    Add a bootstrap system, allowing us to write JS code at early boot
    
    This will be used to write a CommonJS style imports system for JS,
    but it could also be used for other things, like rewriting window.log,
    window.logError, etc. in JS.

 Makefile.am                   |    2 +
 gjs/bootstrap.cpp             |   66 +++++++++++++++++++++++++++++++++++++++++
 gjs/bootstrap.h               |   40 +++++++++++++++++++++++++
 gjs/context.cpp               |    4 ++
 modules/bootstrap.js          |    6 ++++
 modules/modules.gresource.xml |    2 +
 6 files changed, 120 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index ff2c008..835bb86 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -30,6 +30,7 @@ nobase_gjs_public_include_HEADERS =   \
        gjs/gjs.h
 
 nobase_gjs_module_include_HEADERS =    \
+       gjs/bootstrap.h         \
        gjs/gjs-module.h        \
        gjs/compat.h            \
        gjs/coverage.h \
@@ -110,6 +111,7 @@ endif
 libgjs_la_SOURCES =            \
        gjs/byteArray.cpp               \
        gjs/context.cpp         \
+       gjs/bootstrap.cpp       \
        gjs/importer.cpp                \
        gjs/gi.h                \
        gjs/gi.cpp              \
diff --git a/gjs/bootstrap.cpp b/gjs/bootstrap.cpp
new file mode 100644
index 0000000..b049ec4
--- /dev/null
+++ b/gjs/bootstrap.cpp
@@ -0,0 +1,66 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright 2013 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <gjs/gjs.h>
+
+#include "bootstrap.h"
+
+#include <gio/gio.h>
+
+static gboolean
+define_bootstrap_environment(JSContext  *context,
+                             JSObject  **environment_out)
+{
+    JSObject *environment = JS_NewObject(context, NULL, NULL, NULL);
+
+    if (!environment)
+        return FALSE;
+
+    *environment_out = environment;
+    return TRUE;
+}
+
+#define BOOTSTRAP_FILE "resource:///org/gnome/gjs/modules/bootstrap.js"
+
+gboolean
+gjs_run_bootstrap(JSContext *context)
+{
+    GFile *file = g_file_new_for_uri(BOOTSTRAP_FILE);
+    JSObject *environment;
+    char *script = NULL;
+    gsize script_len = 0;
+    jsval script_retval;
+
+    if (!define_bootstrap_environment(context, &environment))
+        return FALSE;
+
+    if (!g_file_load_contents(file, NULL, &script, &script_len, NULL, NULL))
+        return FALSE;
+
+    if (!gjs_eval_with_scope(context, environment, script, script_len, BOOTSTRAP_FILE, NULL))
+        return FALSE;
+
+    return TRUE;
+}
diff --git a/gjs/bootstrap.h b/gjs/bootstrap.h
new file mode 100644
index 0000000..6dde07f
--- /dev/null
+++ b/gjs/bootstrap.h
@@ -0,0 +1,40 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2008  litl, LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef __GJS_BOOTSTRAP_H__
+#define __GJS_BOOTSTRAP_H__
+
+#if !defined (__GJS_GJS_MODULE_H__) && !defined (GJS_COMPILATION)
+#error "Only <gjs/gjs-module.h> can be included directly."
+#endif
+
+#include <glib.h>
+#include "gjs/jsapi-util.h"
+
+G_BEGIN_DECLS
+
+gboolean gjs_run_bootstrap        (JSContext   *context);
+
+G_END_DECLS
+
+#endif  /* __GJS_BOOTSTRAP_H__ */
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 19120c4..6564440 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -26,6 +26,7 @@
 #include <gio/gio.h>
 
 #include "context-private.h"
+#include "bootstrap.h"
 #include "importer.h"
 #include "jsapi-private.h"
 #include "jsapi-util.h"
@@ -460,6 +461,9 @@ gjs_context_constructed(GObject *object)
                                   js_context->global))
         g_error("Failed to point 'imports' property at root importer");
 
+    if (!gjs_run_bootstrap(js_context->context))
+        g_error("Failed to bootstrap GJS context");
+
     JS_EndRequest(js_context->context);
 
     g_mutex_lock (&contexts_lock);
diff --git a/modules/bootstrap.js b/modules/bootstrap.js
new file mode 100644
index 0000000..45ab533
--- /dev/null
+++ b/modules/bootstrap.js
@@ -0,0 +1,6 @@
+(function(exports) {
+    "use strict";
+
+    // Do early initialization here.
+
+})(window);
diff --git a/modules/modules.gresource.xml b/modules/modules.gresource.xml
index 8d2849c..8221d0e 100644
--- a/modules/modules.gresource.xml
+++ b/modules/modules.gresource.xml
@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <gresources>
   <gresource prefix="/org/gnome/gjs">
+    <file>modules/bootstrap.js</file>
+
     <file>modules/tweener/equations.js</file>
     <file>modules/tweener/tweener.js</file>
     <file>modules/tweener/tweenList.js</file>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]