美文网首页我爱编程
Cocos3.14.1 Js 加载探究一

Cocos3.14.1 Js 加载探究一

作者: 甚解_4703 | 来源:发表于2017-04-26 14:47 被阅读0次

最近要去实现一下cocos js 在native的热更新。
仔细研读了下cocos jsb的一些源代码。这里暂时对cocos jsb加载js的过程做一个记录。
本次研究的cocos是cocos3.14.1.


下面开始

先看到cocos源码中的这一段代码:

JS_DefineFunction(cx, global, "require", ScriptingCore::executeScript, 1, JSPROP_PERMANENT);

这是注册jsb环境,注册require全局方法

js实现loadJS方法 ,其实就是调用上面定义的require方法,就是是c++中的ScriptingCore::executeScript方法。

在cocos源码中找到ScriptingCore::executeScript定义位置:
首先获取参数

JS::CallArgs args = JS::CallArgsFromVp(argc, vp);

其次通过

JS::RootedValue jsstr(cx, args.get(0));
JSString* str = JS::ToString(cx, jsstr);
JSStringWrapper path(str);//调用Mozilla提供的JSAPI,里面封装了获取到utf8风格的c字符串。

新手可能会困惑
JSAPI中定义了很多 Handler,MutableHandler,Rooted,PersistentRooted模板,好像还能互相传值。
下面是JSPAI给的英文解释:

- Rooted<T> declares a variable of type T, whose value is always rooted.
 *   Rooted<T> may be automatically coerced to a Handle<T>, below. Rooted<T>
 *   should be used whenever a local variable's value may be held live across a
 *   call which can trigger a GC.
 *
 * - Handle<T> is a const reference to a Rooted<T>. Functions which take GC
 *   things or values as arguments and need to root those arguments should
 *   generally use handles for those arguments and avoid any explicit rooting.
 *   This has two benefits. First, when several such functions call each other
 *   then redundant rooting of multiple copies of the GC thing can be avoided.
 *   Second, if the caller does not pass a rooted value a compile error will be
 *   generated, which is quicker and easier to fix than when relying on a
 *   separate rooting analysis.
 *
 * - MutableHandle<T> is a non-const reference to Rooted<T>. It is used in the
 *   same way as Handle<T> and includes a |set(const T &v)| method to allow
 *   updating the value of the referenced Rooted<T>. A MutableHandle<T> can be
 *   created from a Rooted<T> by using |Rooted<T>::operator&()|.
 *
 * In some cases the small performance overhead of exact rooting (measured to
 * be a few nanoseconds on desktop) is too much. In these cases, try the
 * following:
 *
 * - Move all Rooted<T> above inner loops: this allows you to re-use the root
 *   on each iteration of the loop.
 *
 * - Pass Handle<T> through your hot call stack to avoid re-rooting costs at
 *   every invocation.
 *
 * The following diagram explains the list of supported, implicit type
 * conversions between classes of this family:
 *
 *  Rooted<T> ----> Handle<T>
 *     |               ^
 *     |               |
 *     |               |
 *     +---> MutableHandle<T>
 *     (via &)
 *
 * All of these types have an implicit conversion to raw pointers.

上面是JSAPI对这些类型的一个解释,总结起来就是最后一个示意图,我简单概括就是所有的Handler都是Rooted的常量指针,而MutableHandle也是指向Rooted的指针,但是它是可以修改的原值的。PersistentRooted好像是一个不能被JS GC回收的一个类型,具体我没深入研究,读者可以自行搜索。

接着看源码。

JS::RootedValue jsret(cx);
JS::RootedObject glob(cx, JS::CurrentGlobalOrNull(cx));
res = engine->requireScript(path.get(), glob, cx, &jsret);

编译我们传入的js

auto script = compileScript(path, global, cx);

首先找文件,当然是先从已经加载过的js文件列表中找,
cocos jsb首先会去找jsc文件

std::string byteCodePath = RemoveFileExt(std::string(path)) + ".jsc";
if (filename_script.find(byteCodePath) != filename_script.end())
return filename_script[byteCodePath];

RemoveFileExt只是做了删除后缀的事情。
filename_script保存了已经加载的js字典

然后去寻找 js文件

std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(path);
if (filename_script.find(fullPath) != filename_script.end())
  return filename_script[fullPath];

如果都没找到,那么就返回nullptr

如果在已加载的js中没找到,那么重新new

script = new (std::nothrow) JS::PersistentRootedScript(cx);

同样的,也是先去检查jsc文件

// a) check jsc file first
    std::string byteCodePath = RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT;

cocos2d::FileUtils *futil = cocos2d::FileUtils::getInstance();
if (futil->isFileExist(byteCodePath)){
  //do something
}

下面我们主要讲一下isFileExist这个函数
它会去判断我们的byteCodePath是否是绝对路径,否则就会去寻找我们的搜索目录

for (const auto& searchIt : _searchPathArray)
    {
        for (const auto& resolutionIt : _searchResolutionsOrderArray)
        {
            fullpath = this->getPathForFilename(newFilename, resolutionIt, searchIt);

            if (!fullpath.empty())
            {
                // Using the filename passed in as key.
                _fullPathCache.emplace(filename, fullpath);
                return fullpath;
            }

        }
    }

我用的是windows,查看VS调试信息可知:


搜索目录调试信息

后面会细说我们三个目录的由来

getPathForFilename 这个函数封装了拼接路径,找文件的功能,如果拼接的路径文件不存在,那么返回""

然后继续找.js后缀的文件,

如果找到了,那么存到我们的已加载js字典中

最后注册到我们的JS环境中

evaluatedOK = JS_ExecuteScript(cx, global, (*script), jsvalRet);

script就是我们返回的JS::PersistentRootedScript*对象


现在再来说我们调试出来的三个目录,这个是跟我们project.manifest中定义的搜索目录是相关联的,

"searchPaths": [
        "res"
    ]

由于我在这个文件中设置了res,而我们的cocos自己会定义一个热更新目录,一般各个平台路径都是不一样的,我的是window,那么是在"C:/Users/JJ/AppData/Local/Fish1/"Fish1是我的项目名称)中,所以我们看到第一个目录是 热更新目录+res,第三个目录则是我们的程序运行目录这是默认自带的。cocos优先查找热更新自定义搜索目录,然后是热更新目录,最后才是我们的运行目录,一般运行目录存的是我们打包出去的js文件,如果在这之前,cocos找到相同的文件名会优先加载热更新目录中的js或者资源文件,达到热更新的目的。

相关文章

  • Cocos3.14.1 Js 加载探究一

    最近要去实现一下cocos js 在native的热更新。仔细研读了下cocos jsb的一些源代码。这里暂时对c...

  • iOS底层原理19:类和分类的加载

    前面已经探究了类的加载流程,类分为懒加载类和非懒加载类,他们有不同加载流程,下面来探究下分类的加载,以及分类和类搭...

  • js 控制加载|移除 script 与 link 文件

    js 加载 script 文件 js 加载 link 文件 js 删除 script 文件 js 删除 link 文件

  • js延迟加载的六种方式

    js实现延迟加载的几种方法,js的延迟加载有助与提高页面的加载速度 JS延迟加载,也就是等页面加载完成之后再加载 ...

  • jspath学习资源和脚本编写

    一) 加载js 和 编写js 的过程 将js 文件加入到项目中 加载js 代码 编写脚本 fixInstance...

  • JS延迟加载的几种方式

    JS延迟加载,也就是等页面加载完成之后再加载 JavaScript 文件。 JS延迟加载有助于提高页面加载速度。 ...

  • JS

    JS 懒加载,预加载 概念:懒加载也叫延迟加载:JS图片延迟加载,延迟加载图片或符合某些条件时才加载某些图片。预加...

  • 系统源码简析

    同步加载 异步加载 页面的懒加载,只针对vue文件,js文件被放到app.js中了, 比如 share.js。 a...

  • Android调用js

    Asset目录 Android Studio 中Asset目录 加载Asset中的js js中加载同一目录下的js

  • 前端动态加载JS

    最近机顶盒遇到一种特殊场景需要按需加载js,所以特此记录动态加载JS的方法这个方法加载的是本地的js,如果要加载非...

网友评论

    本文标题:Cocos3.14.1 Js 加载探究一

    本文链接:https://www.haomeiwen.com/subject/ucbxottx.html