{
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);
+}
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)
{
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);
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,
}
gchar *buf = seed_value_to_string (ctx, arguments[0], exception);
- puts(buf);
- g_free (buf);
+ puts (buf);
return JSValueMakeNull (ctx);
}
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,
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);
{
JSGlobalContextRef context;
JSObjectRef global;
+ gchar **search_path;
JSContextGroupRef group;
};
query.param_types[i],
¶ms[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 (¶ms[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;
}
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;
"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)
{
seed_struct_set_property (JSContextRef context,
JSObjectRef object,
JSStringRef property_name,
- JSValueRef value,
- JSValueRef * exception)
+ JSValueRef value, JSValueRef * exception)
{
gint length;
GArgument field_value;
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;
*/
#include "seed-private.h"
-#include <string.h>
#include <dlfcn.h>
JSClassRef gobject_class;
}
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))
{
}
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);
/* 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))
{
}
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);
}
}
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))
{
}
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))
{
{
JSStringRef jsstr = 0;
JSValueRef func, str;
- gchar *buf = 0;
+ gchar *buf = NULL;
gint length;
if (val == NULL)
}
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");
if (jsstr)
JSStringRelease (jsstr);
}
-
return buf;
}
JSValueRef
seed_value_from_filename (JSContextRef ctx,
- const gchar * filename,
- JSValueRef * exception)
+ const gchar * filename, JSValueRef * exception)
{
GError *e = NULL;
gchar *utf8;
}
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);
{
SeedGlobalContext context;
SeedValue global;
+ gchar **search_path;
SeedContextGroup group;
} SeedEngine;
SeedObjectCallAsConstructorCallback
constructor);
+void seed_engine_set_search_path (SeedEngine * eng, gchar ** path);
+
#endif
}
buf = seed_value_to_string(ctx, arguments[0], exception);
-
str = readline(buf);
+
if (str && *str)
{
add_history(str);
#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)
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");
else
seed_exec(argc, argv);
+ g_strfreev(path);
+
return 0;
}
#!/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{