[seed: 7/7] Make the file passed to the seed binary available as imports.filename off the bat to prevent recursi



commit fda06cd21b7e24eeb74f824f96ebd3635c955a7e
Author: Robert Carr <racarr mireia (none)>
Date:   Wed Apr 15 02:23:21 2009 -0400

    Make the file passed to the seed binary available as imports.filename off the bat to prevent recursive running
---
 libseed/seed-importer.c |   33 ++++++++++++++++++++++++++++++---
 libseed/seed.h          |    2 ++
 src/main.c              |    9 ++++++++-
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/libseed/seed-importer.c b/libseed/seed-importer.c
index 057e51f..2a1a840 100644
--- a/libseed/seed-importer.c
+++ b/libseed/seed-importer.c
@@ -1,5 +1,7 @@
 #include <string.h>
 
+#include <gio/gio.h>
+
 #include "seed-private.h"
 
 JSClassRef importer_class;
@@ -533,6 +535,19 @@ seed_importer_handle_native_module (JSContextRef ctx,
   return module_obj;
 }
 
+static gchar *
+seed_importer_canonicalize_path (gchar *path)
+{
+  GFile *file;
+  gchar *absolute_path;
+  
+  file = g_file_new_for_path (path);
+  absolute_path = g_file_get_path (file);
+  g_object_unref (file);
+  
+  return absolute_path;
+}
+
 static JSObjectRef
 seed_importer_handle_file (JSContextRef ctx,
 			   const gchar *dir,
@@ -542,15 +557,18 @@ seed_importer_handle_file (JSContextRef ctx,
   JSContextRef nctx;
   JSObjectRef global, c_global;
   JSStringRef file_contents, file_name;
-  gchar *contents, *walk, *file_path;
+  gchar *contents, *walk, *file_path, *canonical;
   
   file_path = g_strconcat (dir, "/", file, NULL);
+  canonical = seed_importer_canonicalize_path (file_path);
   SEED_NOTE (IMPORTER, "Trying to import file: %s", file_path);
   
-  if (global = g_hash_table_lookup (file_imports, file_path))
+  if (global = g_hash_table_lookup (file_imports, canonical))
     {
       SEED_NOTE (IMPORTER, "Using existing global");
+
       g_free (file_path);
+      g_free (canonical);
       return global;
     }
 
@@ -585,7 +603,8 @@ seed_importer_handle_file (JSContextRef ctx,
   c_global = JSContextGetGlobalObject (ctx);
   JSValueProtect (eng->context, global);
   
-  g_hash_table_insert (file_imports, file_path, global);
+  g_hash_table_insert (file_imports, canonical, global);
+  g_free (file_path);
 
   JSEvaluateScript (nctx, file_contents, c_global, file_name, 0, exception);
 
@@ -818,6 +837,14 @@ JSClassDefinition importer_dir_class_def = {
   NULL				/* Convert To Type */
 };
 
+void
+seed_importer_add_global(JSObjectRef global,
+			 gchar *name)
+{
+  JSValueProtect (eng->context, global);
+  g_hash_table_insert (file_imports, seed_importer_canonicalize_path (name), global);
+}
+
 void seed_initialize_importer(JSContextRef ctx,
 			      JSObjectRef global)
 {
diff --git a/libseed/seed.h b/libseed/seed.h
index a594360..7797a58 100644
--- a/libseed/seed.h
+++ b/libseed/seed.h
@@ -368,4 +368,6 @@ seed_signal_connect_value (SeedContext ctx,
 
 SeedObject seed_context_get_global_object (SeedContext ctx);
 
+void seed_importer_add_global(SeedContext ctx, gchar *name);
+
 #endif
diff --git a/src/main.c b/src/main.c
index 4199b8d..77f01a1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -24,6 +24,8 @@
 #include "../libseed/seed-debug.h"
 #include <girepository.h>
 
+#include <string.h>
+
 #define DEFAULT_PATH "."
 
 SeedEngine *eng;
@@ -47,12 +49,13 @@ seed_repl (gint argc, gchar ** argv)
 void
 seed_exec (gint argc, gchar ** argv)
 {
+  SeedObject global;
   SeedScript *script;
   SeedException e;
   gchar *buffer;
 
   g_file_get_contents (argv[1], &buffer, 0, 0);
-
+  
   if (!buffer)
     {
       g_critical ("File %s not found!", argv[1]);
@@ -75,6 +78,10 @@ seed_exec (gint argc, gchar ** argv)
 		  seed_exception_get_line (eng->context, e));
       exit (1);
     }
+  
+  global = seed_context_get_global_object (eng->context);
+  seed_importer_add_global (global, argv[1]);
+
   seed_evaluate (eng->context, script, 0);
   if ((e = seed_script_exception (script)))
     g_critical ("%s. %s in %s at line %d",



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