Sfoglia il codice sorgente

Add js_module_normalize_path implementation.

BernardXiong 8 anni fa
parent
commit
e92578fc86
3 ha cambiato i file con 142 aggiunte e 6 eliminazioni
  1. 1 2
      rtthread-port/jerry_main.c
  2. 139 4
      rtthread-port/jerry_module.c
  3. 2 0
      rtthread-port/jerry_module.h

+ 1 - 2
rtthread-port/jerry_main.c

@@ -3,7 +3,6 @@
 
 
 #include <rtthread.h>
 #include <rtthread.h>
 #include <finsh.h>
 #include <finsh.h>
-#include <dfs.h>
 
 
 #include <jerryscript.h>
 #include <jerryscript.h>
 #include <jerry_util.h>
 #include <jerry_util.h>
@@ -30,7 +29,7 @@ int jerry_main(int argc, char** argv)
 		char *full_path = NULL;
 		char *full_path = NULL;
 		char *full_dir	= NULL;
 		char *full_dir	= NULL;
 
 
-		full_path = dfs_normalize_path(NULL, argv[1]);
+		full_path = js_module_normalize_path(NULL, argv[1]);
 		full_dir  = js_module_dirname(full_path);
 		full_dir  = js_module_dirname(full_path);
 
 
 		js_set_string_property(global_obj, "__dirname",  full_dir);
 		js_set_string_property(global_obj, "__dirname",  full_dir);

+ 139 - 4
rtthread-port/jerry_module.c

@@ -7,11 +7,17 @@
 
 
 #include "jerry_util.h"
 #include "jerry_util.h"
 #include "jerry_module.h"
 #include "jerry_module.h"
-#include <dfs.h>
 
 
 #include <jerryscript-ext/module.h>
 #include <jerryscript-ext/module.h>
 #include <ecma-globals.h>
 #include <ecma-globals.h>
 
 
+#ifndef PATH_MAX
+#define PATH_MAX	256
+#endif
+
+char *strdup(const char *);
+char *getcwd(char *buf, size_t size);
+
 typedef jerry_value_t (*module_init_func_t)(void);
 typedef jerry_value_t (*module_init_func_t)(void);
 
 
 #ifdef RT_USING_DFS
 #ifdef RT_USING_DFS
@@ -22,7 +28,7 @@ char *js_module_dirname(char *path)
 
 
     if (!path || !*path) return NULL;
     if (!path || !*path) return NULL;
 
 
-	s = rt_strdup(path);
+	s = strdup(path);
 	if (!s) return NULL;
 	if (!s) return NULL;
 
 
     i = strlen(s)-1;
     i = strlen(s)-1;
@@ -35,6 +41,135 @@ __exit:
     return s;
     return s;
 }
 }
 
 
+char *js_module_normalize_path(const char *directory, const char *filename)
+{
+    char *fullpath;
+    char *dst0, *dst, *src;
+	char *cwd = NULL;
+
+	/* check parameters */
+	if (filename == NULL) return NULL;
+
+	if (directory == NULL && filename[0] != '/')
+	{
+		cwd = (char*) malloc (PATH_MAX);
+		if (cwd == NULL) return NULL;
+
+		/* get current working directory */
+		getcwd(cwd, PATH_MAX);
+		directory = cwd;
+	}
+
+    if (filename[0] != '/') /* it's a absolute path, use it directly */
+    {
+        fullpath = malloc(strlen(directory) + strlen(filename) + 2);
+
+        if (fullpath == NULL)
+        {
+        	free(cwd);
+            return NULL;
+        }
+
+        /* join path and file name */
+        snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
+            "%s/%s", directory, filename);
+    }
+    else
+    {
+        fullpath = strdup(filename); /* copy string */
+
+        if (fullpath == NULL)
+            return NULL;
+    }
+
+    src = fullpath;
+    dst = fullpath;
+
+    dst0 = dst;
+    while (1)
+    {
+        char c = *src;
+
+        if (c == '.')
+        {
+            if (!src[1]) src ++; /* '.' and ends */
+            else if (src[1] == '/')
+            {
+                /* './' case */
+                src += 2;
+
+                while ((*src == '/') && (*src != '\0'))
+                    src ++;
+                continue;
+            }
+            else if (src[1] == '.')
+            {
+                if (!src[2])
+                {
+                    /* '..' and ends case */
+                    src += 2;
+                    goto up_one;
+                }
+                else if (src[2] == '/')
+                {
+                    /* '../' case */
+                    src += 3;
+
+                    while ((*src == '/') && (*src != '\0'))
+                        src ++;
+                    goto up_one;
+                }
+            }
+        }
+
+        /* copy up the next '/' and erase all '/' */
+        while ((c = *src++) != '\0' && c != '/')
+            *dst ++ = c;
+
+        if (c == '/')
+        {
+            *dst ++ = '/';
+            while (c == '/')
+                c = *src++;
+
+            src --;
+        }
+        else if (!c)
+            break;
+
+        continue;
+
+up_one:
+        dst --;
+        if (dst < dst0)
+        {
+        	free(cwd);
+            free(fullpath);
+            return NULL;
+        }
+        while (dst0 < dst && dst[-1] != '/')
+            dst --;
+    }
+
+    *dst = '\0';
+
+    /* remove '/' in the end of path if exist */
+    dst --;
+    if ((dst != fullpath) && (*dst == '/'))
+        *dst = '\0';
+
+    /* final check fullpath is not empty, for the special path of lwext "/.." */
+    if ('\0' == fullpath[0])
+    {
+        fullpath[0] = '/';
+        fullpath[1] = '\0';
+    }
+
+	free(cwd);
+
+    return fullpath;
+}
+
 /* load module from file system */
 /* load module from file system */
 static bool load_module_from_filesystem(const jerry_value_t module_name, jerry_value_t *result)
 static bool load_module_from_filesystem(const jerry_value_t module_name, jerry_value_t *result)
 {
 {
@@ -65,11 +200,11 @@ static bool load_module_from_filesystem(const jerry_value_t module_name, jerry_v
 
 
     if (module[0] != '/') /* is a relative path */
     if (module[0] != '/') /* is a relative path */
     {
     {
-        full_path = dfs_normalize_path(dirname, module);
+        full_path = js_module_normalize_path(dirname, module);
     }
     }
     else
     else
     {
     {
-        full_path = dfs_normalize_path(NULL, module);
+        full_path = js_module_normalize_path(NULL, module);
     }
     }
 	free(dirname);
 	free(dirname);
 
 

+ 2 - 0
rtthread-port/jerry_module.h

@@ -2,6 +2,8 @@
 #define JERRY_MODULE_H__
 #define JERRY_MODULE_H__
 
 
 char *js_module_dirname(char *path);
 char *js_module_dirname(char *path);
+char *js_module_normalize_path(const char *directory, const char *filename);
+
 int   js_module_init(void);
 int   js_module_init(void);
 
 
 #endif
 #endif