在 JavaScriptCore 中公开全局对象上的字符串

发布于 2024-11-28 22:32:02 字数 1544 浏览 1 评论 0原文

我已经用 JavaScript 编写了一个可移植框架,并且想在各种 JavaScript 解释器 shell 环境下运行一些性能测试。为了实现此目标,我需要能够将命令行参数 (argv) 传递到脚本上下文。默认情况下,Rhino 和 Spidermonkey 解释器已经执行此操作,将脚本文件后的所有参数公开为绑定到全局对象上的“参数”标识符的数组。最初我的目的是为 v8 示例 shell 以及 JavaScriptCore jsc shell 带来相同的功能,但我很快意识到这需要更多的努力,而且我实际上只需要最后一个命令行参数即可运行我的测试。因此,我已经能够在 v8 中实现此功能,将 argv 中的最后一个 char* 元素转换为 v8::String 并将其绑定到全局对象上的标识符“lastArg”。

不幸的是,我在使用 JavaScriptCore 完成同样的事情时遇到了更多的困难。我还没有找到太多关于 JavaScriptCore C++ API 的文档,以及 JavaScriptCore jsc 解释器中的代码(在 Source/JavaScriptCore/jsc.cpp) 对我来说比 v8 示例 shell 中的代码更难理解。

具体来说,我很感激任何可以帮助说明以下任务的资源(文档、教程、示例代码等):

  • 从 char* 创建新的 JavaScriptCore JSString 实例,
  • 将 JSString 实例绑定到 GlobalObject 全局对象实例上的标识符。

我打算简单地修补 jsc.cpp 的函数 jscmain:

int jscmain(int argc, char** argv, JSGlobalData* globalData)
{
    JSLock lock(SilenceAssertionsOnly);

    Options options;
    parseArguments(argc, argv, options, globalData);

    GlobalObject* globalObject = GlobalObject::create(*globalData, GlobalObject::createStructure(*globalData, jsNull()), options.arguments);

    //TODO: my patch would go here: create a new javascript string, and assign it to an identifier on globalObject instance

    bool success = runWithScripts(globalObject, options.scripts, options.dump);
    if (options.interactive && success)
        runInteractive(globalObject);

    return success ? 0 : 3;
}

我非常感谢任何人可以提供的任何指导。

I've written a portable framework in JavaScript, and I'd like to run some performance tests under various JavaScript interpreter shell environments. In order to accomplish this, I need to be able to pass command-line arguments (argv) to the script context. Rhino and Spidermonkey interpreters do this by default already, exposing all arguments after the script file as an array bound to the the "arguments" identifier on the global object. It was originally my intention to bring the same functionality to the v8 sample shell, as well as the JavaScriptCore jsc shell, but I soon realized that this would require much more effort, and I really only need the last command-line argument in order to run my tests. So, I have been able to get this working in v8, converting the last char* element in argv to a v8::String and binding it to the identifier "lastArg" on the global object.

Unfortunately, I'm having much more trouble accomplishing the same thing with JavaScriptCore. I haven't been able to find much documentation on the JavaScriptCore C++ API, and the code in the JavaScriptCore jsc interpreter (in Source/JavaScriptCore/jsc.cpp) is more difficult for me to understand than the code in the v8 sample shell.

Specifically, I'd appreciate any resources (documentation, tutorials, sample code, etc.) that could help illustrate the following tasks:

  • creating a new JavaScriptCore JSString instance from a char*
  • binding the JSString instance to an identifier on the GlobalObject global object instance.

I intend to simply patch function jscmain of jsc.cpp:

int jscmain(int argc, char** argv, JSGlobalData* globalData)
{
    JSLock lock(SilenceAssertionsOnly);

    Options options;
    parseArguments(argc, argv, options, globalData);

    GlobalObject* globalObject = GlobalObject::create(*globalData, GlobalObject::createStructure(*globalData, jsNull()), options.arguments);

    //TODO: my patch would go here: create a new javascript string, and assign it to an identifier on globalObject instance

    bool success = runWithScripts(globalObject, options.scripts, options.dump);
    if (options.interactive && success)
        runInteractive(globalObject);

    return success ? 0 : 3;
}

I'd greatly appreciate any guidance anyone can offer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2024-12-05 22:32:02

从 char* 创建 JSString:

JSC::JSGlobalData * globalData;

JSString * CreateJSString(const char * chars, size_t length)
{   
    const char * string = chars;
    if (0 == length)
    {
        length = strlen(chars);
    }

    if (isASCII(string, length))
    {
        JSString * jsstr = JSC::jsString(globalData, JSC::UString(string, length));
        return jsstr;
    }

    // Fall through
    return NULL;
}

添加到全局对象:

JSC::JSGlobalObject * globalObject;
JSC::JSGlobalData * globalData;
JSC::Identifier name;
JSC::JSString * str;

globalObject->putDirect(*globalData, name, JSC::JSValue(str));

注意:: 假设您正确创建了存根对象。

To create a JSString from a char*:

JSC::JSGlobalData * globalData;

JSString * CreateJSString(const char * chars, size_t length)
{   
    const char * string = chars;
    if (0 == length)
    {
        length = strlen(chars);
    }

    if (isASCII(string, length))
    {
        JSString * jsstr = JSC::jsString(globalData, JSC::UString(string, length));
        return jsstr;
    }

    // Fall through
    return NULL;
}

Adding to global object:

JSC::JSGlobalObject * globalObject;
JSC::JSGlobalData * globalData;
JSC::Identifier name;
JSC::JSString * str;

globalObject->putDirect(*globalData, name, JSC::JSValue(str));

Note:: Assumes you create the stubbed objects properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文