[seed] [libseed] [os] Make __script_path__ canonical and absolute. Add os.realpath.
- From: Tim Horton <hortont src gnome org>
- To: svn-commits-list gnome org
- Subject: [seed] [libseed] [os] Make __script_path__ canonical and absolute. Add os.realpath.
- Date: Wed, 15 Jul 2009 05:57:38 +0000 (UTC)
commit 8e530b8e5f6bba577709c94e244def26eb862d8f
Author: Tim Horton <hortont svn gnome org>
Date: Wed Jul 15 01:54:11 2009 -0400
[libseed] [os] Make __script_path__ canonical and absolute. Add os.realpath.
extensions/Seed.js.in | 23 ++++++++++++++++++++---
libseed/seed-importer.c | 17 ++++++++++++++---
modules/os/seed-os.c | 23 +++++++++++++++++++++++
3 files changed, 57 insertions(+), 6 deletions(-)
---
diff --git a/extensions/Seed.js.in b/extensions/Seed.js.in
index e8b4584..700b047 100644
--- a/extensions/Seed.js.in
+++ b/extensions/Seed.js.in
@@ -1,3 +1,5 @@
+GLib = imports.gi.GLib;
+
Seed.sprintf = function ()
{
if (typeof arguments == "undefined") { return null; }
@@ -158,12 +160,12 @@ Seed.repl = function()
Seed.glib_repl = function()
{
- imports.gi.GLib.idle_add(Seed.repl, null);
+ GLib.idle_add(Seed.repl, null);
}
Seed.thread_repl = function()
{
- imports.gi.GLib.thread_create_full(function() { while(Seed.repl()){} },
+ GLib.thread_create_full(function() { while(Seed.repl()){} },
null, 0, true);
}
@@ -181,10 +183,25 @@ if(!imports.searchPath || (imports.searchPath.length == 0))
if(Seed.argv.length > 1)
{
- __script_path__ = imports.gi.GLib.path_get_dirname(Seed.argv[1]);
+ __script_path__ = GLib.path_get_dirname(Seed.argv[1]);
+
+ try
+ {
+ if(!GLib.path_is_absolute(__script_path__))
+ {
+ __script_path__ = GLib.build_filenamev([GLib.get_current_dir(),
+ __script_path__]);
+ }
+
+ __script_path__ = imports.os.realpath(__script_path__);
if(__script_path__ && __script_path__ != "")
imports.searchPath.push(__script_path__);
+ }
+ catch(e)
+ {
+ print(imports.JSON.stringify(e));
+ }
}
imports.searchPath.push(".");
diff --git a/libseed/seed-importer.c b/libseed/seed-importer.c
index d33e82d..c8a2668 100644
--- a/libseed/seed-importer.c
+++ b/libseed/seed-importer.c
@@ -595,7 +595,8 @@ seed_importer_handle_file (JSContextRef ctx,
JSValueRef js_file_dirname;
JSObjectRef global, c_global;
JSStringRef file_contents, file_name;
- gchar *contents, *walk, *file_path, *canonical;
+ gchar *contents, *walk, *file_path, *canonical, *absolute_path;
+ gchar normalized_path[PATH_MAX];
file_path = g_strconcat (dir, "/", file, NULL);
canonical = seed_importer_canonicalize_path (file_path);
@@ -641,13 +642,23 @@ seed_importer_handle_file (JSContextRef ctx,
c_global = JSContextGetGlobalObject (ctx);
JSValueProtect (eng->context, global);
- js_file_dirname = seed_value_from_string(ctx, g_path_get_dirname(file_path),
- NULL);
+ absolute_path = g_path_get_dirname(file_path);
+ if(!g_path_is_absolute(absolute_path))
+ {
+ g_free(absolute_path);
+ absolute_path = g_build_filename(g_get_current_dir(),
+ g_path_get_dirname(file_path), NULL);
+ }
+
+ realpath(absolute_path, normalized_path);
+
+ js_file_dirname = seed_value_from_string(ctx, normalized_path, NULL);
seed_object_set_property(nctx, global, "__script_path__", js_file_dirname);
g_hash_table_insert (file_imports, canonical, global);
g_free (file_path);
+ g_free (absolute_path);
JSEvaluateScript (nctx, file_contents, NULL, file_name, 0, exception);
diff --git a/modules/os/seed-os.c b/modules/os/seed-os.c
index db7b8f7..4b93a56 100644
--- a/modules/os/seed-os.c
+++ b/modules/os/seed-os.c
@@ -28,6 +28,28 @@ SeedObject os_namespace;
return seed_make_undefined (ctx);
SeedValue
+seed_os_realpath (SeedContext ctx,
+ SeedObject function,
+ SeedObject this_object,
+ size_t argument_count,
+ const SeedValue arguments[],
+ SeedException * exception)
+{
+ gchar *arg;
+ gchar ret[PATH_MAX];
+
+ if (argument_count != 1)
+ {
+ EXPECTED_EXCEPTION("os.realpath", "1 argument");
+ }
+ arg = seed_value_to_string (ctx, arguments[0], exception);
+ realpath(arg, ret);
+ g_free (arg);
+
+ return seed_value_from_string (ctx, ret, exception);
+}
+
+SeedValue
seed_os_chdir (SeedContext ctx,
SeedObject function,
SeedObject this_object,
@@ -981,6 +1003,7 @@ seed_os_fork (SeedContext ctx,
seed_static_function os_funcs[] = {
{"fork", seed_os_fork, 0},
+ {"realpath", seed_os_realpath, 0},
{"chdir", seed_os_chdir, 0},
{"fchdir", seed_os_fchdir, 0},
{"getcwd", seed_os_getcwd, 0},
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]