Patch from Matt to add global include paths. Can't set from JS, but can from C, and...
authorTim Horton <hortont@hortont.com>
Tue, 24 Feb 2009 17:46:25 +0000 (12:46 -0500)
committerTim Horton <hortont424@gmail.com>
Tue, 24 Feb 2009 17:46:25 +0000 (12:46 -0500)
libseed/seed-api.c
libseed/seed-builtins.c
libseed/seed-engine.c
libseed/seed-private.h
libseed/seed-signals.c
libseed/seed-structs.c
libseed/seed-types.c
libseed/seed.h
modules/readline/seed-readline.c
src/main.c
tests/javascript/builtin-argument-length.js

index 42e07fe..e209032 100644 (file)
@@ -316,3 +316,19 @@ seed_value_is_function (JSContextRef ctx, JSObjectRef value)
 {
   return seed_value_is_object (ctx, value) && JSObjectIsFunction (ctx, value);
 }
+
+/**
+ * seed_engine_set_search_path:
+ * @eng: A #SeedEngine, to set the path on.
+ *
+ * @path: A #gchar**, A null terminated list of strings containing path
+ *
+ * Return value: void
+ *
+ */
+void
+seed_engine_set_search_path (SeedEngine * eng, gchar ** path)
+{
+  g_free (eng->search_path);   /* this should be null from seed_init unless there's already a path set. */
+  eng->search_path = g_strdupv (path);
+}
index b41bec8..d9c8c6b 100644 (file)
@@ -28,8 +28,10 @@ seed_include (JSContextRef ctx,
              const JSValueRef arguments[], JSValueRef * exception)
 {
   JSStringRef file_contents, file_name;
-  gchar *import_file;
+  GDir *dir;
+  gchar *import_file, *abs_path;
   gchar *buffer, *walk;
+  guint i;
 
   if (argumentCount != 1)
     {
@@ -40,13 +42,47 @@ seed_include (JSContextRef ctx,
       g_free (mes);
       return JSValueMakeNull (ctx);
     }
+
   import_file = seed_value_to_string (ctx, arguments[0], exception);
 
-  g_file_get_contents (import_file, &buffer, 0, 0);
+  /* just try current dir if no path set, or use the absolute path */
+  if (!eng->search_path || g_path_is_absolute (import_file))
+    g_file_get_contents (import_file, &buffer, 0, NULL);
+  else                         /* A search path is set and path given is not absolute.  */
+    {
+      for (i = 0; i < g_strv_length (eng->search_path); ++i)
+       {
+         dir = g_dir_open (eng->search_path[i], 0, NULL);
+
+         if (!dir)             /* skip bad path entries, but exception */
+           {
+             gchar *mes =
+               g_strdup_printf ("Cannot open path entry: %s",
+                                eng->search_path[i]);
+             seed_make_exception (ctx, exception, "CannotOpenPathEntry",
+                                  mes);
+             g_free (mes);
+             continue;
+           }
+
+         abs_path =
+           g_build_filename (eng->search_path[i], import_file, NULL);
+
+         if (g_file_get_contents (abs_path, &buffer, 0, NULL))
+           {
+             g_dir_close (dir);
+             g_free (abs_path);
+             break;
+           }
+
+         g_dir_close (dir);
+         g_free (abs_path);
+       }
+    }
 
   if (!buffer)
     {
-      gchar *mes = g_strdup_printf ("File not found: %s", import_file);
+      gchar *mes = g_strdup_printf ("Failed to open file: %s", import_file);
       seed_make_exception (ctx, exception, "FileNotFound", mes);
 
       g_free (import_file);
@@ -80,6 +116,85 @@ seed_include (JSContextRef ctx,
   return JSValueMakeNull (ctx);
 }
 
+static JSValueRef
+seed_get_include_path (JSContextRef ctx,
+                      JSObjectRef function,
+                      JSObjectRef this_object,
+                      size_t argumentCount,
+                      const JSValueRef arguments[], JSValueRef * exception)
+{
+  JSValueRef ret;
+  if (argumentCount > 0)
+    {
+      gchar *mes =
+       g_strdup_printf ("Seed.get_include_path expected "
+                        "0 arguments, got %Zd",
+                        argumentCount);
+      seed_make_exception (ctx, exception, "ArgumentError", mes);
+      g_free (mes);
+      return (JSValueMakeNull (ctx));
+    }
+
+  /* also do I want this to give a list? seems more useful. */
+
+  gchar *path_string = g_strjoinv (":", eng->search_path);
+  ret = seed_value_from_string (ctx, path_string, exception);
+  g_free (path_string);
+
+  return ret;
+}
+
+#if 0
+static JSValueRef
+seed_set_include_path (JSContextRef ctx,
+                      JSObjectRef function,
+                      JSObjectRef this_object,
+                      size_t argumentCount,
+                      const JSValueRef arguments[], JSValueRef * exception)
+{
+  guint i;
+
+  if (argumentCount == 0)
+    {
+      gchar *mes =
+       g_strdup_printf ("Seed.set_include_path expected "
+                        "> 0 arguments, got %Zd",
+                        argumentCount);
+      seed_make_exception (ctx, exception, "ArgumentError", mes);
+      g_free (mes);
+      return JSValueMakeNull (ctx);
+    }
+
+  g_print ("going to free context's search path.: %p\n", eng->search_path);
+
+  gchar *t = g_strjoinv (":", eng->search_path);
+  g_print ("about to free '%s'\n", t);
+  g_free (t);
+
+  g_strfreev (eng->search_path);       /* free the old path. */
+  g_print ("allocating new eng->search_path\n");
+  eng->search_path = (gchar **) g_malloc (argumentCount + 1);  /* args + NULL terminator */
+  g_print ("new eng->search_path: %p\n", eng->search_path);
+
+  for (i = 0; i < argumentCount; ++i)
+    {
+      g_print ("yay argcount: i: %d %p\n", i, arguments[i]);
+      eng->search_path[i] =
+       seed_value_to_string (ctx, arguments[i], exception);
+    }
+
+  eng->search_path[argumentCount] = NULL;      /* null terminator in last position. */
+
+  g_print ("New path length is : %d\n", g_strv_length (eng->search_path));
+
+  t = g_strjoinv (":", eng->search_path);
+  g_print ("path is '%s'\n", t);
+  g_free (t);
+
+  return JSValueMakeNull (ctx);
+}
+#endif
+
 static JSValueRef
 seed_print (JSContextRef ctx,
            JSObjectRef function,
@@ -98,8 +213,7 @@ seed_print (JSContextRef ctx,
     }
 
   gchar *buf = seed_value_to_string (ctx, arguments[0], exception);
-  puts(buf);
-  g_free (buf);
+  puts (buf);
 
   return JSValueMakeNull (ctx);
 }
@@ -305,6 +419,9 @@ seed_init_builtins (SeedEngine * local_eng, gint * argc, gchar *** argv)
                                            local_eng->global,
                                            "Seed");
 
+  //seed_create_function(local_eng->context, "set_include_path", &seed_set_include_path, obj);
+  seed_create_function (local_eng->context, "get_include_path",
+                       &seed_get_include_path, obj);
   seed_create_function (local_eng->context, "include", &seed_include, obj);
   seed_create_function (local_eng->context, "print", &seed_print, obj);
   seed_create_function (local_eng->context,
index 6c86855..315f640 100644 (file)
@@ -1463,6 +1463,7 @@ seed_init (gint * argc, gchar *** argv)
   eng->context = JSGlobalContextCreateInGroup (context_group, NULL);
   eng->global = JSContextGetGlobalObject (eng->context);
   eng->group = context_group;
+  eng->search_path = NULL;
 
   gobject_class = JSClassCreate (&gobject_def);
   JSClassRetain (gobject_class);
index 5fb8a57..9a6bb69 100644 (file)
@@ -37,6 +37,7 @@ struct _SeedEngine
 {
   JSGlobalContextRef context;
   JSObjectRef global;
+  gchar **search_path;
 
   JSContextGroupRef group;
 };
index e3484a1..3e5c315 100644 (file)
@@ -330,19 +330,18 @@ seed_gobject_signal_emit (JSContextRef ctx,
                                 query.param_types[i],
                                 &params[i + 1], exception);
 
-  
   if (query.return_type != G_TYPE_NONE)
-         g_value_init(&ret_value, query.return_type);
+    g_value_init (&ret_value, query.return_type);
   g_signal_emitv (params, privates->signal_id, 0, &ret_value);
 
   for (i = 0; i < argumentCount; i++)
     g_value_unset (&params[i]);
   g_free (params);
-  
+
   ret = seed_value_from_gvalue (ctx, &ret_value, exception);
-  
+
   if (query.return_type != G_TYPE_NONE)
-         g_value_unset(&ret_value);
+    g_value_unset (&ret_value);
 
   return ret;
 }
index 7022350..f193ff2 100644 (file)
@@ -211,9 +211,9 @@ seed_union_get_property (JSContextRef context,
 
 static bool
 seed_union_set_property (JSContextRef context,
-                         JSObjectRef object,
-                         JSStringRef property_name,
-                         JSValueRef value, JSValueRef * exception)
+                        JSObjectRef object,
+                        JSStringRef property_name,
+                        JSValueRef value, JSValueRef * exception)
 {
   gint length;
   GArgument field_value;
@@ -232,8 +232,7 @@ seed_union_set_property (JSContextRef context,
             "with name %s \n",
             g_base_info_get_name (priv->info), cproperty_name);
 
-  field =
-    seed_union_find_field ((GIUnionInfo *) priv->info, cproperty_name);
+  field = seed_union_find_field ((GIUnionInfo *) priv->info, cproperty_name);
 
   if (!field)
     {
@@ -255,8 +254,7 @@ static bool
 seed_struct_set_property (JSContextRef context,
                          JSObjectRef object,
                          JSStringRef property_name,
-                         JSValueRef value, 
-                         JSValueRef * exception)
+                         JSValueRef value, JSValueRef * exception)
 {
   gint length;
   GArgument field_value;
@@ -297,8 +295,7 @@ seed_struct_set_property (JSContextRef context,
 static JSValueRef
 seed_struct_get_property (JSContextRef context,
                          JSObjectRef object,
-                         JSStringRef property_name,
-                         JSValueRef * exception)
+                         JSStringRef property_name, JSValueRef * exception)
 {
   gchar *cproperty_name;
   int length;
index 09f479c..06f4172 100644 (file)
@@ -16,7 +16,6 @@
  */
 
 #include "seed-private.h"
-#include <string.h>
 #include <dlfcn.h>
 
 JSClassRef gobject_class;
@@ -840,8 +839,8 @@ seed_gi_argument_make_js (JSContextRef ctx,
 }
 
 JSValueRef
-seed_value_from_gvalue (JSContextRef ctx,
-                       GValue * gval, JSValueRef * exception)
+seed_value_from_gvalue (JSContextRef ctx, GValue * gval,
+                       JSValueRef * exception)
 {
   if (!G_IS_VALUE (gval))
     {
@@ -1145,8 +1144,8 @@ seed_gvalue_from_seed_value (JSContextRef ctx,
 }
 
 JSValueRef
-seed_object_get_property (JSContextRef ctx,
-                         JSObjectRef val, const gchar * name)
+seed_object_get_property (JSContextRef ctx, JSObjectRef val,
+                         const gchar * name)
 {
 
   JSStringRef jname = JSStringCreateWithUTF8CString (name);
@@ -1178,8 +1177,8 @@ seed_object_set_property (JSContextRef ctx, JSObjectRef object,
 /* TODO: Make some macros or something for making exceptions, code is littered
    with annoyingness right now */
 gboolean
-seed_value_to_boolean (JSContextRef ctx,
-                      JSValueRef val, JSValueRef * exception)
+seed_value_to_boolean (JSContextRef ctx, JSValueRef val,
+                      JSValueRef * exception)
 {
   if (!JSValueIsBoolean (ctx, val))
     {
@@ -1197,8 +1196,8 @@ seed_value_to_boolean (JSContextRef ctx,
 }
 
 JSValueRef
-seed_value_from_boolean (JSContextRef ctx,
-                        gboolean val, JSValueRef * exception)
+seed_value_from_boolean (JSContextRef ctx, gboolean val,
+                        JSValueRef * exception)
 {
   return JSValueMakeBoolean (ctx, val);
 }
@@ -1371,8 +1370,8 @@ seed_value_from_int64 (JSContextRef ctx, gint64 val, JSValueRef * exception)
 }
 
 guint64
-seed_value_to_uint64 (JSContextRef ctx,
-                     JSValueRef val, JSValueRef * exception)
+seed_value_to_uint64 (JSContextRef ctx, JSValueRef val,
+                     JSValueRef * exception)
 {
   if (!JSValueIsNumber (ctx, val))
     {
@@ -1414,8 +1413,8 @@ seed_value_from_float (JSContextRef ctx, gfloat val, JSValueRef * exception)
 }
 
 gdouble
-seed_value_to_double (JSContextRef ctx,
-                     JSValueRef val, JSValueRef * exception)
+seed_value_to_double (JSContextRef ctx, JSValueRef val,
+                     JSValueRef * exception)
 {
   if (!JSValueIsNumber (ctx, val))
     {
@@ -1440,7 +1439,7 @@ seed_value_to_string (JSContextRef ctx,
 {
   JSStringRef jsstr = 0;
   JSValueRef func, str;
-  gchar *buf = 0;
+  gchar *buf = NULL;
   gint length;
 
   if (val == NULL)
@@ -1452,12 +1451,11 @@ seed_value_to_string (JSContextRef ctx,
     }
   else if (JSValueIsNull (ctx, val) || JSValueIsUndefined (ctx, val))
     {
-      buf = strdup ("[null]");
+      buf = g_strdup ("[null]");
     }
   else
     {
-      if (!JSValueIsString (ctx, val)) // In this case,
-       // it's an object
+      if (!JSValueIsString (ctx, val)) /* In this case, it's an object */
        {
          func =
            seed_object_get_property (ctx, (JSObjectRef) val, "toString");
@@ -1476,7 +1474,6 @@ seed_value_to_string (JSContextRef ctx,
       if (jsstr)
        JSStringRelease (jsstr);
     }
-
   return buf;
 }
 
@@ -1493,8 +1490,7 @@ seed_value_from_string (JSContextRef ctx,
 
 JSValueRef
 seed_value_from_filename (JSContextRef ctx,
-                         const gchar * filename, 
-                         JSValueRef * exception)
+                         const gchar * filename, JSValueRef * exception)
 {
   GError *e = NULL;
   gchar *utf8;
@@ -1565,8 +1561,8 @@ seed_value_to_object (JSContextRef ctx,
 }
 
 JSValueRef
-seed_value_from_object (JSContextRef ctx,
-                       GObject * val, JSValueRef * exception)
+seed_value_from_object (JSContextRef ctx, GObject * val,
+                       JSValueRef * exception)
 {
   if (val == NULL)
     return JSValueMakeNull (ctx);
index fc78514..62834aa 100644 (file)
@@ -66,6 +66,7 @@ typedef struct _SeedEngine
 {
   SeedGlobalContext context;
   SeedValue global;
+  gchar **search_path;
 
   SeedContextGroup group;
 } SeedEngine;
@@ -342,4 +343,6 @@ SeedObject seed_make_constructor (SeedContext ctx,
                                  SeedObjectCallAsConstructorCallback
                                  constructor);
 
+void seed_engine_set_search_path (SeedEngine * eng, gchar ** path);
+
 #endif
index d8f9daf..2a6aaaa 100644 (file)
@@ -104,8 +104,8 @@ seed_readline(SeedContext ctx,
        }
 
        buf = seed_value_to_string(ctx, arguments[0], exception);
-
        str = readline(buf);
+
        if (str && *str)
        {
                add_history(str);
index e0a8d12..51f4e7f 100644 (file)
@@ -24,6 +24,8 @@
 #include "../libseed/seed-debug.h"
 #include <girepository.h>
 
+#define DEFAULT_PATH ".:/usr/share/doc/seed/examples"
+
 SeedEngine *eng;
 
 void seed_repl(gint argc, gchar **argv)
@@ -83,10 +85,15 @@ void seed_exec(gint argc, gchar ** argv)
 
 gint main(gint argc, gchar ** argv)
 {
+       gchar** path;
        g_set_prgname("seed");
-       g_thread_init(0);
+       g_thread_init(NULL);
        eng = seed_init(&argc, &argv);
 
+       path = g_strsplit(DEFAULT_PATH, ":", -1);
+
+       seed_engine_set_search_path(eng, path);
+
        if (!g_irepository_require(g_irepository_get_default(), "GObject", 0, 0, 0))
                g_critical("Unable to import GObject repository");
 
@@ -95,6 +102,8 @@ gint main(gint argc, gchar ** argv)
        else
                seed_exec(argc, argv);
 
+       g_strfreev(path);
+
        return 0;
 }
 
index 3d9cd72..5dbf7dc 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env seed
 // Returns: 0
 // STDIN:
-// STDOUT:Seed\.spawn expected 1 argument\nFailed to execute child process "asihfieuhgieuhgw" \(No such file or directory\)\nSeed\.include expected 1 argument, got 0\nFile not found: 1\.000000\nFile not found: \nSeed\.include expected 1 argument, got 2\nSeed\.include expected 1 argument, got 2\nSeed\.print expected 1 argument, got 0\nSeed\.print expected 1 argument, got 2\nSeed\.print expected 1 argument, got 2\nSeed\.print expected 1 argument, got 3\nSeed\.introspect expected 1 argument, got 0\nSeed\.introspect expected 1 argument, got 2\nSeed\.check_syntax expected 1 argument, got 0\nSeed\.check_syntax expected 1 argument, got 2
+// STDOUT:Seed\.spawn expected 1 argument\nFailed to execute child process "asihfieuhgieuhgw" \(No such file or directory\)\nSeed\.include expected 1 argument, got 0\nFailed to open file: 1\.000000\nFailed to open file: \nSeed\.include expected 1 argument, got 2\nSeed\.include expected 1 argument, got 2\nSeed\.print expected 1 argument, got 0\nSeed\.print expected 1 argument, got 2\nSeed\.print expected 1 argument, got 2\nSeed\.print expected 1 argument, got 3\nSeed\.introspect expected 1 argument, got 0\nSeed\.introspect expected 1 argument, got 2\nSeed\.check_syntax expected 1 argument, got 0\nSeed\.check_syntax expected 1 argument, got 2
 // STDERR:
 
 try{