[gjs/wip/require: 25/29] Add a bootstrap system, allowing us to write JS code at early boot



commit 8d4e1bfda5fb2e73799edf9df8b1af74da9a1ac9
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                |   65 ++++++++++++++++++++++++++++++++++++++
 gjs/bootstrap.h                  |   40 +++++++++++++++++++++++
 gjs/context.cpp                  |    4 ++
 modules/bootstrap.js             |    6 +++
 modules/modules.gresource.xml.in |    2 +
 6 files changed, 119 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 5059966..dfd72df 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -27,6 +27,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/byteArray.h         \
@@ -103,6 +104,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..5270e8b
--- /dev/null
+++ b/gjs/bootstrap.cpp
@@ -0,0 +1,65 @@
+/* -*- 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;
+}
+
+gboolean
+gjs_run_bootstrap(JSContext *context)
+{
+    GFile *file = g_file_new_for_uri("resource:///org/gnome/gjs/modules/bootstrap.js");
+    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>", NULL, 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 afa8a38..77fa6d1 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -26,6 +26,7 @@
 #include <gio/gio.h>
 
 #include "context.h"
+#include "bootstrap.h"
 #include "importer.h"
 #include "jsapi-util.h"
 #include "native.h"
@@ -478,6 +479,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.in b/modules/modules.gresource.xml.in
index 8fccdfd..b53eafa 100644
--- a/modules/modules.gresource.xml.in
+++ b/modules/modules.gresource.xml.in
@@ -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]