C++对象与Javascript对象生命周期不同步

发布于 2024-09-16 14:51:32 字数 702 浏览 3 评论 0原文

我有一个关于 C++ Object & 的问题Javascript 对象生命周期不同步,希望各位大师能够帮助我。

具体来说,我将 SpiderMonkey 嵌入到我的 C++ 程序中。我不确定我的做法是否正确。我正在做的事情就像

(1)C++程序加载一个Javascript文件并调用一个入口函数。

(2)在入口函数中,调用一些C++函数来初始化程序。在这些调用期间,将创建一些 C++ 对象。

(3) 基于用户交互,将调用相应的脚本函数作为事件处理程序。

这种方法有效,但有一个问题(可能还有更多未识别的问题:)

也就是说,

在我的 JS 事件处理函数中,我需要将 C++ 对象获取到 Javascript 上下文并调用它的成员函数。为此,在我的 C++ 获取函数中,我检查其相应的 JS 对等对象是否已创建。如果没有,我使用 JS_NewObject 创建一个并返回它。然后JS函数就可以调用它的native函数了。

问题在于,在某些情况下,此类本机函数调用的结果会导致 C++ 对象死亡。但是,我找不到一种方法来通知 JS 上下文也删除其 JS 对等对象。我找不到 JSAPI 函数来执行此操作。

在我当前的程序中,当最终 JS 运行时被销毁时,所有使用 JS_NewObject 创建的 JS 对象都被销毁。

我猜这与 SipderMonkey 的“垃圾收集”有关。但我还没有找到好的指南。非常感谢您的任何建议

I got a question regarding C++ Object & Javascript Object life cycle mis-synchronization and hope here your gurus could help me out.

Specifically, I am embedding SpiderMonkey in my C++ program. I am not sure if my approach is right or not. What I am doing is like

(1) C++ program loads a Javascript file and call an entry function.

(2) In the entry function, some C++ functions are called to initialize the program. During these calls, some C++ objects will be created.

(3) Based on user interaction, corresponding script function will be called as event handler.

This approach works, but with one problem (there could be unidentified more :)

That is,

In my JS event handler function, I need to fetch a C++ object to the Javascript context and call it member function. For this, in my C++ fetching function, I check if its corresponding JS peer object has been created or not. If not, I use JS_NewObject to create one and return it. Then the JS function could call native function on it.

The problem is that in some cases, the result of such native function call will lead the death of the C++ object. However, I can not find a way to notify the JS context to delete its JS peer object too. I failed to find a JSAPI function to do so.

In my current program, all the JS objects created using JS_NewObject are destroyed when finally the JS runtime is destroyed.

I guess this has something do with SipderMonkey's "garbage collection". But I have not found a good guide yet. Many thanks for any suggestionto

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

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

发布评论

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

评论(1

拥醉 2024-09-23 14:51:32

JS 是一个 GC 环境,因此您不能简单地“删除”GC 分配的对象。基本上有 2 个选项可供选择:

  1. 使 C++ 对象依赖于 JS 包装器对象,如果您使用引用计数,则在创建包装器时会增加 C++ 对象的 ref,并减少 JS 包装器中的 ref包装器对象终结器。

  2. 当您销毁 C++ 对象时,获取包装器对象(如果存在)以清除对 C++ 对象的引用。现在,您的所有回调都需要在使用 C++ 对象之前进行 null 检查,但您不会崩溃(您可能会抛出 JS 异常作为响应?)

在大多数情况下,选项 1 是用户所期望的。

我会指出所需的 API,但我不知道 SM API(我知道 JSC API,但它们适用相同的概念)

JS is a GC'd environment so you can't simply "delete" a GC allocated object. There are basically 2 options you can take:

  1. Make your C++ object be dependent on the JS wrapper object, if you were using refcounting for instance you would increment the C++ object's ref when you created a wrapper, and decrement the ref in the wrapper objects finalizer.

  2. When you destroy the C++ object, fetch the wrapper object (if it exists) as clear the reference to the C++ object. All your callbacks will now need to null check prior to using the C++ object, but you won't crash (you could throw a JS exception in response perhaps?)

In most cases option 1 is what users expect.

I'd point to the required API but i don't know the SM API (I know the JSC API instead, but they same concepts apply)

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