예제 #1
0
        private static duk_ret_t cb_load_module(IntPtr ctx)
        {
            var module_id = DuktapeAux.duk_require_string(ctx, 0);

            DuktapeDLL.duk_get_prop_string(ctx, 2, "filename");
            var filename = DuktapeAux.duk_require_string(ctx, -1);
            var source   = GetVM(ctx)._fileResolver.ReadAllBytes(filename);

            // Debug.LogFormat("cb_load_module module_id:'{0}', filename:'{1}', resolved:'{2}'\n", module_id, filename, resolvedPath);
            do
            {
                if (source != null && source.Length > 0) // bytecode is unsupported
                {
                    if (source[0] != 0xbf)
                    {
                        DuktapeDLL.duk_unity_push_lstring(ctx, source, (uint)source.Length);
                    }
                    else
                    {
                        DuktapeDLL.duk_type_error(ctx, "cannot load module (bytecode): %s", module_id);
                    }
                    break;
                }
                DuktapeDLL.duk_type_error(ctx, "cannot load module: %s", module_id);
            } while (false);
            return(1);
        }
예제 #2
0
        public static int duk_dofile(IntPtr ctx)
        {
            var filename = DuktapeAux.duk_require_string(ctx, 0);

            DuktapeVM.GetVM(ctx).EvalFile(filename);
            return(0);
        }
예제 #3
0
        public static int duk_addSearchPath(IntPtr ctx)
        {
            var path = DuktapeAux.duk_require_string(ctx, 0);

            DuktapeVM.GetVM(ctx).AddSearchPath(path);
            return(0);
        }
예제 #4
0
        private static duk_ret_t cb_resolve_module(IntPtr ctx)
        {
            var module_id  = EnsureExtension(DuktapeAux.duk_require_string(ctx, 0));
            var parent_id  = DuktapeAux.duk_require_string(ctx, 1);
            var resolve_to = module_id;

            // Debug.LogFormat("cb_resolve_module module_id:'{0}', parent_id:'{1}'\n", module_id, parent_id);

            if (module_id.StartsWith("./") || module_id.StartsWith("../") || module_id.Contains("/./") || module_id.Contains("/../"))
            {
                // 显式相对路径直接从 parent 模块路径拼接
                var parent_path = PathUtils.GetDirectoryName(parent_id);
                try
                {
                    resolve_to = PathUtils.ExtractPath(PathUtils.Combine(parent_path, module_id), '/');
                }
                catch
                {
                    // 不能提升到源代码目录外面
                    DuktapeDLL.duk_type_error(ctx, "invalid module path (out of sourceRoot): %s", module_id);
                    return(1);
                }
            }
            // Debug.LogFormat("resolve_cb(1): id:{0}', parent-id:'{1}', resolve-to:'{2}'", module_id, parent_id, resolve_to);
            // if (GetVM(ctx).ResolvePath(resolve_to) == null)
            // {
            //     DuktapeDLL.duk_type_error(ctx, "cannot find module: %s", module_id);
            //     return 1;
            // }

            if (resolve_to != null)
            {
                DuktapeDLL.duk_push_string(ctx, resolve_to);
            }
            else
            {
                DuktapeDLL.duk_type_error(ctx, "cannot find module: %s", module_id);
            }
            return(1);
        }
예제 #5
0
        private static duk_ret_t cb_load_module(IntPtr ctx)
        {
            var module_id = DuktapeAux.duk_require_string(ctx, 0);

            DuktapeDLL.duk_get_prop_string(ctx, 2, "filename");
            var filename     = DuktapeAux.duk_require_string(ctx, -1);
            var resolvedPath = GetVM(ctx).ResolvePath(filename);
            // Debug.LogFormat("cb_load_module module_id:'{0}', filename:'{1}', resolved:'{2}'\n", module_id, filename, resolvedPath);

            var source = GetVM(ctx)._fileManager.ReadAllText(resolvedPath);

            if (source != null)
            {
                DuktapeDLL.duk_push_string(ctx, source);
            }
            else
            {
                DuktapeDLL.duk_type_error(ctx, "cannot load module: %s", module_id);
            }

            return(1);
        }