Re: libseed-list imports.searchPath



Here's the patch. It does the below, including replacing '.' with current script_path if set. It also shrank the source by 55 lines.

I've tested it, and everything works as expected. Please apply!

PS: something that didn't work before but now does, is that a dir_importer object will now allow importing of any existing libseed_xxx.so modules in that dir.

Regards
/Jonatan

Jonatan Liljedahl wrote:
Looking at the importer, I see a lot of things I'd like to do:

seed_importer_search() and seed_importer_dir_get_property() actually does the same thing, except seed_importer_search() does it once for each searchpath. I'll do a seed_importer_search_in_dir(dirpath, prop) that is called by both.

In this seed_importer_search_in_dir() I will instead of walking through all directory entries simply:

- is dirpath/prop a file or dir? pass it to seed_importer_handle_file()
- is dirpath/prop.js a file? pass it to seed_importer_handle_file()
- is dirpath/libseed_prop.so a file? pass it to seed_importer_handle_native_module()
- else return NULL

Note the check for .js extension. I really think this is the way to do it, you don't want it to load an old foo.js~ backup file or foo.data or whatever.

Patch coming soon!

/Jonatan

Jonatan Liljedahl wrote:
...
for the importer you could try changing the
seed-importer.c:seed_importer_search

from dir = g_dir_open ((gchar *) walk->data, 0, &e);


to something like this. gchar* sp = seed_value_to_string (ctx, seed_object_get_property (nctx, global, "__script_path__"); exception);

gchar* test_path = (gchar *) walk->data; if (strcmp(test_path, ".")
== 0) { test_path = sp; }

And see how it behaves. Regards Alan

Ok, will try something out.
/Jonatan


--- seed-importer.c.original	2010-06-18 12:18:16.000000000 +0200
+++ seed-importer.c	2010-06-18 12:20:09.000000000 +0200
@@ -718,95 +718,79 @@
 }
 
 static JSObjectRef
-seed_importer_search (JSContextRef ctx, gchar * prop, JSValueRef * exception)
+seed_importer_search_dirs (JSContextRef ctx, GSList *path, gchar *prop, JSValueRef *exception)
 {
+    GSList *walk;
+    JSObjectRef ret, global;
+    JSValueRef script_path_prop;
+    gchar *prop_as_lib, *prop_as_js, *script_path;
+
+    prop_as_lib = g_strconcat ("libseed_", prop, ".", G_MODULE_SUFFIX, NULL);
+    prop_as_js = g_strconcat (prop, ".js", NULL);
+
+    // get the current script_path      
+    global = JSContextGetGlobalObject (ctx);
+    script_path_prop = seed_object_get_property (ctx, global, "__script_path__");
+    if (script_path_prop==NULL || JSValueIsUndefined (ctx, script_path_prop))
+        script_path = NULL;
+    else
+        script_path = seed_value_to_string (ctx, script_path_prop, exception);
+
+    ret = NULL;
+    walk = path;
+    while (walk) {
+        gchar *test_path = walk->data;
+        gchar *file_path;
+        
+        // replace '.' with current script_path if not null
+        if(script_path && !g_strcmp0(".",test_path))
+            test_path = script_path;
+
+        // check if prop is a file or dir (imports['foo.js'] or imports.mydir)
+        file_path = g_build_filename (test_path, prop, NULL);
+        if (g_file_test (file_path, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_DIR)) {
+            ret = seed_importer_handle_file (ctx, test_path, prop, exception);
+            g_free (file_path);
+            break;
+        }
+        g_free (file_path);
 
-  GDir *dir;
-  GError *e;
-  gchar *mentry, *file_path, *file_basename, *file_dirname;
-  GSList *path, *walk;
-  JSObjectRef ret;
-  gchar *prop_as_lib =
-    g_strconcat ("libseed_", prop, ".", G_MODULE_SUFFIX, NULL);
-  gsize i, mentrylen;
-
-  path = seed_importer_get_search_path (ctx, exception);
-
-  walk = path;
-  while (walk)
-    {
-      e = NULL;
-      const gchar *entry;
-
-      dir = g_dir_open ((gchar *) walk->data, 0, &e);
-      if (e)
-	{
-	  g_error_free (e);
-
-	  walk = walk->next;
-	  continue;
-	}
-      // try as as string first - eg. imports['xxxx.js']
-      file_path = g_build_filename ((gchar *) walk->data, prop, NULL);
-      // we could check first here if file already has been loaded
-      // skipping another state (see importer above..)
+        // check if prop is file ending with '.js'
+        file_path = g_build_filename (test_path, prop_as_js, NULL);
+        if (g_file_test (file_path, G_FILE_TEST_IS_REGULAR)) {
+            ret = seed_importer_handle_file (ctx, test_path, prop_as_js, exception);
+            g_free (file_path);
+            break;
+        }
+        g_free (file_path);
       
-      if (g_file_test (file_path, G_FILE_TEST_IS_REGULAR)) 
-        {
-          file_dirname = g_path_get_dirname(file_path);
-          file_basename = g_path_get_basename(file_path);
-          ret = seed_importer_handle_file ( ctx,  file_dirname, file_basename, exception);
-          g_free (file_path);
-          g_free (file_basename);
-          g_free (file_dirname);
-          return ret;
+        // check if file is native module
+        file_path = g_build_filename (test_path, prop_as_lib, NULL);
+        if (g_file_test (file_path, G_FILE_TEST_IS_REGULAR)) {
+            ret = seed_importer_handle_native_module (ctx, test_path, prop_as_lib, exception);
+            g_free (file_path);
+            break;
         }
-      g_free (file_path);
-      while ((entry = g_dir_read_name (dir)))
-	{
-	  mentry = g_strdup (entry);
-
-	  mentrylen = strlen (mentry);
-	  for (i = 0; i < mentrylen; i++)
-	    {
-	      if (mentry[i] == '.')
-		mentry[i] = '\0';
-	    }
-	  if (!g_strcmp0 (mentry, prop))
-	    {
-	      ret =
-		seed_importer_handle_file (ctx, walk->data, entry, exception);
-
-	      g_dir_close (dir);
-	      g_free (mentry);
-	      g_free (prop_as_lib);
-	      seed_importer_free_search_path (path);
-
-	      return ret;
-	    }
-	  else if (!g_strcmp0 (entry, prop_as_lib))
-	    {
-	      ret =
-		seed_importer_handle_native_module (ctx, walk->data, entry,
-						    exception);
-	      g_dir_close (dir);
-	      g_free (mentry);
-	      g_free (prop_as_lib);
-	      seed_importer_free_search_path (path);
-
-	      return ret;
-	    }
-
-	  g_free (mentry);
-	}
-      g_dir_close (dir);
+        g_free (file_path);
 
-      walk = walk->next;
+        walk = walk->next;
     }
 
-  seed_importer_free_search_path (path);
-  g_free (prop_as_lib);
-  return NULL;
+    g_free (prop_as_lib);
+    g_free (prop_as_js);
+    g_free (script_path);
+    
+    return ret;
+}
+
+static JSObjectRef
+seed_importer_search (JSContextRef ctx, gchar *prop, JSValueRef *exception)
+{
+    JSObjectRef ret = NULL;
+    GSList *path = seed_importer_get_search_path (ctx, exception);
+    ret = seed_importer_search_dirs (ctx, path, prop, exception);
+    seed_importer_free_search_path (path);
+    return ret;
 }
 
 static JSValueRef
@@ -835,59 +819,20 @@
 }
 
 static JSValueRef
-seed_importer_dir_get_property (JSContextRef ctx,
-				JSObjectRef object,
-				JSStringRef property_name,
-				JSValueRef * exception)
+seed_importer_dir_get_property (JSContextRef ctx, JSObjectRef object, JSStringRef property_name, JSValueRef *exception)
 {
-  GError *e = NULL;
-  GDir *dir;
-  guint len, i;
-  gsize mentrylen;
-  const gchar *entry;
-  gchar *dir_path, *prop, *mentry;
-  JSObjectRef ret;
-
-  dir_path = JSObjectGetPrivate (object);
-
-  len = JSStringGetMaximumUTF8CStringSize (property_name);
-  prop = g_alloca (len * sizeof (gchar));
-  JSStringGetUTF8CString (property_name, prop, len);
-
-  // TODO: GError
-  dir = g_dir_open (dir_path, 0, &e);
-  if (e)
-    {
-      seed_make_exception_from_gerror (ctx, exception, e);
-      g_error_free (e);
-
-      return NULL;
-    }
-  while ((entry = g_dir_read_name (dir)))
-    {
-      mentry = g_strdup (entry);
-      mentrylen = strlen (mentry);
-
-      for (i = 0; i < mentrylen; i++)
-	{
-	  if (mentry[i] == '.')
-	    mentry[i] = '\0';
-	}
-
-      if (!g_strcmp0 (mentry, prop))
-	{
-
-	  ret = seed_importer_handle_file (ctx, dir_path, entry, exception);
-	  g_dir_close (dir);
-	  g_free (mentry);
-
-	  return ret;
-	}
-      g_free (mentry);
-    }
-  g_dir_close (dir);
-
-  return NULL;
+    guint len;
+    gchar *prop;
+    GSList path;
+
+    path.data = JSObjectGetPrivate (object);
+    path.next = NULL;
+
+    len = JSStringGetMaximumUTF8CStringSize (property_name);
+    prop = g_alloca (len * sizeof (gchar));
+    JSStringGetUTF8CString (property_name, prop, len);
+    
+    return seed_importer_search_dirs(ctx, &path, prop, exception);
 }
 
 static void


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