[seed] Add sandbox module, allows creation of sandboxed JS contexts, with a global object which you can man



commit 38a11de0a90aabe236acf1d27f08bfd7ae5299c7
Author: Robert Carr <racarr svn gnome org>
Date:   Wed May 6 21:20:16 2009 -0400

    Add sandbox module, allows creation of sandboxed JS contexts, with a global object which you can manipulate. It is also possible to fill the global object with the default seed globals
---
 libseed/seed-api.c        |    8 ++-
 libseed/seed.h            |    2 +-
 modules/sandbox/sandbox.c |   92 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 96 insertions(+), 6 deletions(-)

diff --git a/libseed/seed-api.c b/libseed/seed-api.c
index 0017b88..80a9012 100644
--- a/libseed/seed-api.c
+++ b/libseed/seed-api.c
@@ -276,6 +276,7 @@ seed_evaluate (JSContextRef ctx,
  * seed_simple_evaluate:
  * @ctx: A #SeedContext.
  * @source: A string representing the JavaScript to evaluate.
+ * @exception: A #SeedException pointer to store an exception in.
  *
  * Evaluates a string of JavaScript.
  *
@@ -284,12 +285,13 @@ seed_evaluate (JSContextRef ctx,
  */
 JSValueRef
 seed_simple_evaluate (JSContextRef ctx, 
-		      const gchar * source)
+		      const gchar * source,
+		      JSValueRef *exception)
 {
   JSValueRef ret;
   JSStringRef script = JSStringCreateWithUTF8CString (source);
 
-  ret = JSEvaluateScript (ctx, script, NULL, NULL, 0, NULL);
+  ret = JSEvaluateScript (ctx, script, NULL, NULL, 0, exception);
 
   JSStringRelease (script);
   return ret;
@@ -566,7 +568,7 @@ seed_signal_connect (JSContextRef ctx,
 {
   JSValueRef func;
   
-  func = seed_simple_evaluate(ctx, script);
+  func = seed_simple_evaluate(ctx, script, NULL);
   seed_signal_connect_full(ctx, object, signal, (JSObjectRef)func,
 			   NULL);
 }
diff --git a/libseed/seed.h b/libseed/seed.h
index d70f65d..cb28c3e 100644
--- a/libseed/seed.h
+++ b/libseed/seed.h
@@ -78,7 +78,7 @@ SeedEngine *seed_init (gint *argc, gchar ***argv);
 SeedEngine *seed_init_with_context_group (gint *argc, gchar ***argv,
 					  SeedContextGroup group);
 
-SeedValue seed_simple_evaluate (SeedContext ctx, gchar * source);
+SeedValue seed_simple_evaluate (SeedContext ctx, gchar * source, SeedException *exception);
 
 SeedScript *seed_make_script (SeedContext ctx,
 			      const gchar * js, const gchar * source_url,
diff --git a/modules/sandbox/sandbox.c b/modules/sandbox/sandbox.c
index 97c2d36..3125010 100644
--- a/modules/sandbox/sandbox.c
+++ b/modules/sandbox/sandbox.c
@@ -1,8 +1,96 @@
 #include <seed.h>
 
+SeedContext ctx;
+SeedContextGroup group;
+SeedObject namespace_ref;
+SeedClass context_class;
+
+static SeedObject
+seed_construct_sandbox_context (SeedContext ctx,
+				SeedObject constructor,
+				size_t argument_count,
+				const SeedValue arguments[],
+				SeedException * exception)
+{
+  SeedObject ret;
+  SeedContext c;
+  
+  c = seed_context_create (group, NULL);
+  ret = seed_make_object (ctx, context_class, c);
+  
+  seed_object_set_property (ctx, ret, "global", seed_context_get_global_object (c));
+  return ret;
+}
+
+static SeedValue
+seed_context_eval (SeedContext ctx,
+		  SeedObject function,
+		  SeedObject this_object,
+		  size_t argument_count,
+		  const SeedValue arguments[], 
+		  SeedException * exception)
+{
+  SeedContext c = seed_object_get_private (this_object);
+  SeedValue ret;
+  gchar *s;
+  
+  s = seed_value_to_string (ctx, arguments[0], exception);
+  ret = seed_simple_evaluate (c, s, exception);
+  g_free (s);
+  
+  return ret;
+}
+
+static SeedValue
+seed_sandbox_context_add_globals (SeedContext ctx,
+				  SeedObject function,
+				  SeedObject this_object,
+				  size_t argument_count,
+				  const SeedValue arguments[], 
+				  SeedException * exception)
+{
+  SeedContext c = seed_object_get_private (this_object);
+  seed_prepare_global_context (c);
+  
+  return seed_make_null (ctx);
+}
+
+seed_static_function context_funcs[] = {
+  {"eval", seed_context_eval, 0},
+  {"add_globals", seed_sandbox_context_add_globals, 0},
+  {0, 0, 0}
+};
+
+static void
+context_finalize (SeedObject object)
+{
+  SeedContext *c = seed_object_get_private (object);
+  
+  seed_context_unref (c);
+}
+
 SeedObject
 seed_module_init(SeedEngine * eng)
 {
-  g_printf("Hello Seed Module World \n");
-  return seed_make_object (eng->context, NULL, NULL);
+  SeedObject context_constructor;
+  seed_class_definition context_class_def = seed_empty_class;
+
+  ctx = eng->context;
+  group = eng->group;
+  namespace_ref = seed_make_object (ctx, NULL, NULL);
+  
+  context_class_def.class_name = "Context";
+  context_class_def.static_functions = context_funcs;
+  context_class_def.finalize = context_finalize;
+  
+  context_class = seed_create_class (&context_class_def);
+  
+  
+  context_constructor = seed_make_constructor (eng->context,
+					       context_class,
+					       seed_construct_sandbox_context);
+  seed_object_set_property (eng->context, namespace_ref, "Context", context_constructor);
+  
+  return namespace_ref;
+
 }



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