简体   繁体   English

V8引擎中的Typeof

[英]Typeof in V8 engine

I am experimentation with V8 engine, i am not a C programmer and it is very difficult for me .But i need this thing for myself 我正在使用V8引擎进行实验,我不是C程序员,这对我来说很难。但是我自己需要这个东西

I change a function RUNTIME_FUNCTION(Runtime_Typeof) ; 我更改一个函数RUNTIME_FUNCTION(Runtime_Typeof) ; (runtime-object.cc) And now if to create proxy in JS: (runtime-object.cc)现在如果要在JS中创建代理:

prx = Proxy.create({
 get:function(k,v){
    if( v == '%type_of%'){
       return 'string'
    }
    .......
 }
})

typeof prx  -> "string"

Ok, i want this effect 好吧,我想要这种效果

BUT if try to make compare: 但是,如果尝试进行比较:

  typeof prx == 'string' -> false =(
  typeof prx == 'object' -> true
  (x = typeof prx) == 'string' -> true

After many hours i found this: In V8 ( full-codegen-x64.cc ) exist this func void FullCodeGenerator :: EmitLiteralCompareTypeof 几个小时后,我发现了这一点:在V8( full-codegen-x64.cc )中,存在这个函数void FullCodeGenerator :: EmitLiteralCompareTypeof

It is look like an optimizator for expression 看起来像是表达的优化器

   typeof prx == "string" || "number" || "object" and etc..
   if (String::Equals(check, factory->number_string())) {
   ...
   } else if (String::Equals(check, factory->string_string())) {
   and etc...

it do not call Runtime_Typeof and 它不调用Runtime_Typeof

    typeof prx -> "string"
    typeof prx == 'string' -> false =(

How can i check if typeof prx it is proxy, then v8 must to call Runtime_Typeof .. 我如何检查typeof prx是否为代理,则v8必须调用Runtime_Typeof

if typeof prx -> string, then typeof == 'string' must be true! 如果typeof prx -> string, then typeof == 'string' must be true! Help me please 请帮帮我

You would need to emit code that distinguishes the cases at runtime . 您将需要在运行时 发出区分情况的代码 That is not too hard, but not a completely trivial change either. 这不是太难,但也不是完全不重要的改变。 Moreover, you'd also need to adapt the optimising compiler. 此外,您还需要调整优化编译器。

But that's the easy part. 但这是容易的部分。 Changing the meaning of typeof will break other parts of the system, which rely on the correct semantics. 更改typeof的含义将破坏系统的其他部分,这些部分依赖于正确的语义。 For example, all uses of the IS_STRING primitive in V8's built-in libraries will become incorrect -- you are likely to introduce crashes that way. 例如,V8内置库中对IS_STRING原语的所有使用将变得不正确-您可能会以这种方式引入崩溃。

Even if you managed to fix all that, the idea would be very questionable from a language semantics perspective. 即使您设法解决了所有这些问题,但从语言语义学的角度来看,这个想法还是很可疑的。 There is a reason why proxies cannot fake strings, first and foremost, strings aren't objects. 代理不能伪造字符串是有原因的,首先,字符串不是对象。 You would also break JavaScript code that relies on the spec'ed behaviour. 您还将破坏依赖于指定行为的JavaScript代码。

In short: don't . 简而言之: 不要 It's a really bad idea. 这是一个非常糟糕的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM