libseed-list Hooking into the imports system



Here is a small patch that adds a feature that I'd be very happy to see.
It allows one to hook into the imports system by defining a handler for
when a module was not found. Example:

  imports.__notFoundHandler__ = function(name, dir) {
    print("making "+name);
    return {foo:123}
  }
  f = imports.foobar;
  print(f.foo);

running above file prints:

  making foobar
  123

This can be used to integrate all sorts of cool stuff with the imports
object. For example, the handler can iterate over imports.searchPath to
find n+".html" and return an object representation of the DOM tree, or
parsing a custom language (hmmmm ;)), or perhaps even connect to an
online database of modules.. x = imports.seedgems.foo; -> updates foo
from an online repository, etc...

Currently it only passes the first path from seed_importer_search_dirs() as the second arg, at least this works for the case when the search is done inside a single directory, but I guess it would be better to just send the whole path GSList by converting it to a JS array. One could still work around it on the script side by merging the dir arg with imports.searchPath if dir != searchPath[0].

BTW, this patch also adds the toString and toValue hack to seed_importer_dir_get_property() so it doesn't call the handler saying that "toString" module wasn't found, etc..

/Jonatan


diff --git a/libseed/seed-importer.c b/libseed/seed-importer.c
index 421f71e..f5efc01 100644
--- a/libseed/seed-importer.c
+++ b/libseed/seed-importer.c
@@ -795,6 +795,16 @@ seed_importer_search_dirs (JSContextRef ctx, GSList *path, gchar *prop, JSValueR
     g_free (prop_as_js);
     g_free (script_path);
     
+    if(ret == NULL) {
+      JSValueRef handler = seed_object_get_property (ctx, importer, "__notFoundHandler__");
+      if (JSValueIsObject (ctx, handler)) {
+	JSValueRef args[2] = {
+	  seed_value_from_string (ctx, prop, exception),
+	  seed_value_from_string (ctx, path->data, exception)};
+	return (JSObjectRef) JSObjectCallAsFunction (ctx, JSValueToObject(ctx,handler,exception), NULL, 2, args, exception);
+      }
+    }
+
     return ret;
 }
 
@@ -825,6 +835,8 @@ seed_importer_get_property (JSContextRef ctx,
     return gi_importer;
   if (!g_strcmp0 (prop, "searchPath"))
     return NULL;
+  if (!g_strcmp0 (prop, "__notFoundHandler__"))
+    return NULL;
   if (!g_strcmp0 (prop, "toString"))	// HACK
     return NULL;
 
@@ -849,6 +861,11 @@ seed_importer_dir_get_property (JSContextRef ctx,
     len = JSStringGetMaximumUTF8CStringSize (property_name);
     prop = g_alloca (len * sizeof (gchar));
     JSStringGetUTF8CString (property_name, prop, len);
+
+    if (!g_strcmp0 (prop, "toString"))	// HACK
+        return NULL;
+    if (!g_strcmp0 (prop, "valueOf"))	// HACK
+        return NULL;
     
     return seed_importer_search_dirs(ctx, &path, prop, exception);
 }


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