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



commit fe1807f19ba57c94e8cfa092ad91ad059e47eec3
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             |   50 +++++++++++++++++++++++++++++++++++++++++
 gjs/bootstrap.h               |   37 ++++++++++++++++++++++++++++++
 gjs/context.cpp               |    4 +++
 modules/bootstrap.js          |    6 +++++
 modules/modules.gresource.xml |    2 +
 6 files changed, 101 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index cfe2089..f781f35 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -75,6 +75,8 @@ libgjs_la_LIBADD += $(GJS_GTK_LIBS)
 endif
 
 libgjs_la_SOURCES =            \
+       gjs/bootstrap.cpp               \
+       gjs/bootstrap.h                 \
        gjs/byteArray.cpp               \
        gjs/context.cpp         \
        gjs/context-private.h           \
diff --git a/gjs/bootstrap.cpp b/gjs/bootstrap.cpp
new file mode 100644
index 0000000..8c6f4f5
--- /dev/null
+++ b/gjs/bootstrap.cpp
@@ -0,0 +1,50 @@
+/* -*- 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 <gio/gio.h>
+
+#include "bootstrap.h"
+#include "gjs/jsapi-util.h"
+#include "gjs/jsapi-wrapper.h"
+
+#define BOOTSTRAP_FILE "resource:///org/gnome/gjs/modules/bootstrap.js"
+
+bool
+gjs_run_bootstrap(JSContext *cx)
+{
+    JS::RootedObject environment(cx,
+        JS_NewObject(cx, NULL, JS::NullPtr(), JS::NullPtr()));
+
+    g_autoptr(GFile) file = g_file_new_for_uri(BOOTSTRAP_FILE);
+    g_autofree char *script = NULL;
+    size_t script_len = 0;
+
+    if (!g_file_load_contents(file, NULL, &script, &script_len, NULL, NULL))
+        return false;
+
+    JS::RootedValue ignored(cx);
+    return gjs_eval_with_scope(cx, environment, script, script_len,
+                               BOOTSTRAP_FILE, &ignored);
+}
diff --git a/gjs/bootstrap.h b/gjs/bootstrap.h
new file mode 100644
index 0000000..ba33153
--- /dev/null
+++ b/gjs/bootstrap.h
@@ -0,0 +1,37 @@
+/* -*- 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__
+
+#include <glib.h>
+
+#include "gjs/jsapi-wrapper.h"
+
+G_BEGIN_DECLS
+
+bool gjs_run_bootstrap(JSContext *cx);
+
+G_END_DECLS
+
+#endif  /* __GJS_BOOTSTRAP_H__ */
diff --git a/gjs/context.cpp b/gjs/context.cpp
index e814841..9208bc0 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-constructor-proxy.h"
 #include "jsapi-private.h"
@@ -516,6 +517,9 @@ gjs_context_constructed(GObject *object)
     if(!gjs_define_promise_object(js_context->context, global))
         g_error("Failed to define global Promise object");
 
+    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 6aa5eb8..ef7a1b3 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]