有没有办法将JSValue移动到新的JSContext中忽略其原始上下文?

时间:2014-01-05 20:12:55

标签: javascript ios objective-c c javascriptcore

我有两个JSContexts,我想不时地在它们之间交换JSValues。但是,如果可能的话,我很难将JSValue移动到新的上下文中。

我正在尝试这个:

newContext[@"newValue"] = [JSValue valueWithObject:newValue inContext:newContext];

虽然新上下文现在具有该值,但该值仍保留其旧上下文。不幸的是,它仍然保留了旧的背景。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

我建议您在新的上下文中创建新的JSValue之前,将JSValue的值从旧的javascript上下文提取到普通的objective-c对象中。查看JSValue.h表明JSValue类具有只读属性,该属性包含值来自的JSContext。

我无法确定您在上面的代码中处理的是什么类型的值,但是例如(对于简单类型):

NSString *newString = [newValue toString]; // Extract from old JSValue
newContext[@"newValue"] = newString;

或更复杂的对象:

@protocol MyPointExports <JSExport>
@property double x;
@property double y;
@end

@interface MyPoint : NSObject <MyPointExports>
// Put methods and properties not visible to JavaScript code here.
@end

newcontext[@"MyPoint"] = [MyPoint class]; // Define the class in Javascript

...

MyPoint *p = [newValue toObject]; // Extract from old JSValue
newContext[@"newValue"] = p;

请注意,该值仍将存在于旧的JSContext中(旧的JSContext将保留活动状态,同时保留旧值)。您可能希望通过以下方式删除此引用:

oldContext[@"oldValue"] = nil; // Assuming the var in the oldContext was called oldValue

另请注意,您不需要使用构造函数:

+ (JSValue *)valueWithObject:(id)value inContext:(JSContext *)context;

因为JavaScriptCore具有以下内置转换(请参阅JSValue.h):

  Objective-C type  |   JavaScript type
--------------------+---------------------
        nil         |     undefined
       NSNull       |        null
      NSString      |       string
      NSNumber      |   number, boolean
    NSDictionary    |   Object object
      NSArray       |    Array object
       NSDate       |     Date object
      NSBlock *     |   Function object *
         id **      |   Wrapper object **
       Class ***    | Constructor object ***

* Instances of NSBlock with supported arguments types will be presented to
JavaScript as a callable Function object. For more information on supported
argument types see JSExport.h. If a JavaScript Function originating from an
Objective-C block is converted back to an Objective-C object the block will
be returned. All other JavaScript functions will be converted in the same
manner as a JavaScript object of type Object.

** For Objective-C instances that do not derive from the set of types listed
above, a wrapper object to provide a retaining handle to the Objective-C
instance from JavaScript. For more information on these wrapper objects, see
JSExport.h. When a JavaScript wrapper object is converted back to Objective-C
the Objective-C instance being retained by the wrapper is returned.

*** For Objective-C Class objects a constructor object containing exported
class methods will be returned. See JSExport.h for more information on
constructor objects.

答案 1 :(得分:0)

实际上,您不必担心JSValue中的上下文,如果在javascript代码中放置断点,您可以看到从Native传递的对象没有上下文属性。 JSValue中的上下文仅用于显示原始JSContext,并且实际上并未链接到它。