SpiderMonkey:我如何从c中获取当前JSObject的名称?

发布于 2024-09-27 11:44:42 字数 496 浏览 2 评论 0原文

有人问如何获取来自 c 的 JSObject 属性。这对我有一点帮助。

但是,有谁知道如何从 c 中获取对象的当前 JavaScript 名称?

示例:

var foo={prop:'bar'};

然后在 jsapi.cpp 中的某个地方:

JS_somemethod(JSContext *cx, JSObject *obj){

//how do i get the name 'foo' (or the current alias) here if *obj points to the foo (or whatever alias name) object?

}

感谢提示和答案!

someone asked how to get the value of a JSObject property from c. That helped me a bit.

But, does anyone know how to get the current JavaScript name of an object from c?

example:

var foo={prop:'bar'};

then somewhere for example in jsapi.cpp:

JS_somemethod(JSContext *cx, JSObject *obj){

//how do i get the name 'foo' (or the current alias) here if *obj points to the foo (or whatever alias name) object?

}

Thx for hints and answers!

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

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

发布评论

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

评论(2

当梦初醒 2024-10-04 11:44:42

好的,为了记录您的评论中的问题澄清,我将在这里重复一遍:

也许我可以简单地告诉您我的目的:为了在网络浏览器中集成新的安全系统,我需要找出在网站上的公共会话期间访问的内容。我的目标是获得诸如日志之类的内容,哪些对象被访问以及如何(读、写、执行)。例如: window.alert (x) window.foo.bar (w) ...你现在为什么我需要变量的名称?也许您有其他想法?

预先说明一下,这通常是相当棘手的。有几个选项可供您使用,所有这些选项都有些困难:

  1. 使用调试 API,可以通过 cool新的调试器对象 API 或通过 jsdbgapi.cpp(firebug 使用的复杂且略显粗糙的接口)。您可以使用调试器功能来捕获所有属性访问、函数调用和本地引用,以便转储对象的有趣属性。
  2. 如果您只对访问您的(用户定义的)对象的方式感兴趣,则可以使用 代理 包装您的对象并转储对其执行的访问。实际上,浏览器中使用了稍微强大的低级代理版本来实现我们的安全功能。 (此外,大多数代理功能都可以通过我们的元对象协议在 JSAPI 中自定义创建的对象进行访问,但代理只是同一功能的更简洁的版本。)
  3. 在 jsinterp.cpp 中检测解释器循环。许多类似研究的工作会关闭 JIT 并检测解释器循环,因为如果您以前研究过语言实现,这是相当容易理解的。不幸的是,即使您有语言实现的经验,一开始将您想要执行的操作转换为解释器循环检测并不总是那么容易。

调试器对象绝对是我在这里列出的选项中最喜欢的选项。如果您采用这种方法并最终陷入困境,请随意访问 irc.mozilla.org 中的 #jsapi 并尝试获取 jorendorff 或 jimb,他们 a) 非常棒,b) 调试器对象的创建者。

Okay, just to document the question clarification from your comment I'll repeat it here:

Maybe i tell you in short my purpose: For the integration of new security system in webbrowsers i need to find out what is accessed during a common session on a website. my aim is to get something like a log which objects are accessed and how (read,write,execute). for example: window.alert (x) window.foo.bar (w) ... you now why i need the names of the variables? maybe you've got an other idea?

Just to say it up front, this is pretty tricky in general. There are a few options available to you, all of which are somewhat difficult:

  1. Use the debugging API, either via the cool new debugger object API or via jsdbgapi.cpp (the complex and slightly gross interface that firebug uses). You can use debugger functionality to trap on all property accesses, functions calls, and local references in order to dump out interesting properties of objects.
  2. If you're only interested in the way that your (user defined) objects are accessed, you can use Proxies that wrap your objects and dump out the accesses being performed on them. A slightly more powerful and low-level version of proxies are actually used in the browser to implement our security features. (Additionally, most of the proxy functionality is accessible for custom-created objects in the JSAPI via our meta-object-protocol, but proxies are just a much cleaner version of that same functionality.)
  3. Instrument the interpreter loop in jsinterp.cpp. A lot of research-like work turns off the JITs and instruments the interpreter loop, because it's fairly understandable if you've worked on language implementations before. Unfortunately, it's not always easy to translate what you want to do into interpreter loop instrumentation at first, even if you have experience with language implementations.

The debugger object is definitely my favorite option of those I've listed here. If you go with that approach and end up getting stuck feel free to visit #jsapi in irc.mozilla.org and try to get a hold of jorendorff or jimb, they're a) awesome and b) the creators of the debugger object.

┾廆蒐ゝ 2024-10-04 11:44:42

在不仔细研究 API 的情况下,我想说的是,虽然在一些非常基本的情况下可能可以这样做,但您不想这样做。这就是为什么:

在 Javascript 中,与大多数语言一样,变量指向值。变量不是值本身。该对象与名称 foo 没有任何内在联系。

例如,您可以执行

var foo = {prop: 'bar'};
var fooo = foo;
var foooo = foo;
var quux = {q: foo, r: foo, s: [foo]};

所有这些 foo 现在都是完全相同的 foo;你想找回哪一件?没有对此进行 API 调用,因为它太含糊而且不是很有用。

但是,如果您确实想找到一个保存值的全局变量,您可以尝试迭代全局对象上的键并测试它们的值。

for(var key in this){
  if(this[key] == myval){
    var myname = key;
  }
}

您必须将其转换为 API 调用,或者将其放入函数中并通过 API 调用该函数

更简单直接的解决方案是弄清楚您稍后想要对 foo 做什么,并传入一个回调函数来执行此操作,例如使用 JS_CallFunction

tl;dr:上一段。

Without looking too closely at the API, I'm going to say that while it might be possible to do this in some very basic cases, you don't want to. This is why:

In Javascript, as in most languages, variables point to values. Variables are not the values themselves. The object is not in any way inherently related to the name foo.

For instance, you can do

var foo = {prop: 'bar'};
var fooo = foo;
var foooo = foo;
var quux = {q: foo, r: foo, s: [foo]};

All of those foos are now the exact same foo; which one do you want back? There's no API call for that because it's too ambiguous and not terribly useful.

However, if you really want to find one of the global variables holding a value, you can try iterating over the keys on the global object and testing them for the value.

for(var key in this){
  if(this[key] == myval){
    var myname = key;
  }
}

You'd have to translate this into your API calls or else put it in a function and call that through the API.

The more simple and straightforward solution would be to figure out what you want to do with foo later on, and pass in a callback function that will do that, e.g. with JS_CallFunction.

tl;dr: Previous paragraph.

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