iOS js 使用与JSContext
JSContext:js执行环境,包含了js执行时所需要的所有函数和对象;
js执行时,会在执行环境搜索需要的函数然后执行,或者保存传入的变量或函数;
JSContext *jsContext = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
jsContext[@"sayhi"] = ^(NSString *name) {
NSLog(@"say hi to %@",name);
};
<div data-role="collapsible">
<h3 onclick="sayhi('systemVersion')">systemVersion</h3>
<p id="systemVersion"></p>
</div>
* thread #7: tid = 0x15e622, 0x000000010f557f13 jsTest`__27-[ViewController refreshXX]_block_invoke(.block_descriptor=<unavailable>, name=@"systemVersion") + 51 at ViewController.m:70, name = 'WebThread', stop reason = breakpoint 1.1
* frame #0: 0x000000010f557f13 jsTest`__27-[ViewController refreshXX]_block_invoke(.block_descriptor=<unavailable>, name=@"systemVersion") + 51 at ViewController.m:70
frame #1: 0x00000001106145cc CoreFoundation`__invoking___ + 140
frame #2: 0x000000011061441e CoreFoundation`-[NSInvocation invoke] + 286
frame #3: 0x000000010f9cedb3 JavaScriptCore`JSC::ObjCCallbackFunctionImpl::call(JSContext*, OpaqueJSValue*, unsigned long, OpaqueJSValue const* const*, OpaqueJSValue const**) + 451
frame #4: 0x000000010f9ce926 JavaScriptCore`JSC::objCCallbackFunctionCallAsFunction(OpaqueJSContext const*, OpaqueJSValue*, OpaqueJSValue*, unsigned long, OpaqueJSValue const* const*, OpaqueJSValue const**) + 262
frame #5: 0x000000010f9cfbad JavaScriptCore`long long JSC::APICallbackFunction::call<JSC::ObjCCallbackFunction>(JSC::ExecState*) + 573
frame #6: 0x000000010f999340 JavaScriptCore`JSC::LLInt::setUpCall(JSC::ExecState*, JSC::Instruction*, JSC::CodeSpecializationKind, JSC::JSValue, JSC::LLIntCallLinkInfo*) + 528
frame #7: 0x000000010f9a035d JavaScriptCore`llint_entry + 22900
frame #8: 0x000000010f99a7d9 JavaScriptCore`vmEntryToJavaScript + 326
frame #9: 0x000000010f86c959 JavaScriptCore`JSC::JITCode::execute(JSC::VM*, JSC::ProtoCallFrame*) + 169
frame #10: 0x000000010f8535ad JavaScriptCore`JSC::Interpreter::executeCall(JSC::ExecState*, JSC::JSObject*, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&) + 493
frame #11: 0x000000010f627bd7 JavaScriptCore`JSC::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&, WTF::NakedPtr<JSC::Exception>&) + 71
frame #12: 0x00000001158f5c1a WebCore`WebCore::JSEventListener::handleEvent(WebCore::ScriptExecutionContext*, WebCore::Event*) + 1002
frame #13: 0x0000000115542630 WebCore`WebCore::EventTarget::fireEventListeners(WebCore::Event*, WebCore::EventTargetData*, WTF::Vector<WebCore::RegisteredEventListener, 1ul, WTF::CrashOnOverflow, 16ul>&) + 752
frame #14: 0x0000000115542267 WebCore`WebCore::EventTarget::fireEventListeners(WebCore::Event*) + 311
frame #15: 0x000000011552b50e WebCore`WebCore::MouseOrFocusEventContext::handleLocalEvents(WebCore::Event&) const + 174
frame #16: 0x000000011552c0b8 WebCore`WebCore::EventDispatcher::dispatchEvent(WebCore::Node*, WTF::PassRefPtr<WebCore::Event>) + 920
frame #17: 0x0000000115c2dc5d WebCore`WebCore::Node::dispatchEvent(WTF::PassRefPtr<WebCore::Event>) + 29
frame #18: 0x000000011551561e WebCore`WebCore::Element::dispatchMouseEvent(WebCore::PlatformMouseEvent const&, WTF::AtomicString const&, int, WebCore::Element*) + 318
frame #19: 0x00000001155328df WebCore`WebCore::EventHandler::dispatchMouseEvent(WTF::AtomicString const&, WebCore::Node*, bool, int, WebCore::PlatformMouseEvent const&, bool) + 111
frame #20: 0x0000000115534108 WebCore`WebCore::EventHandler::handleMouseReleaseEvent(WebCore::PlatformMouseEvent const&) + 1192
frame #21: 0x000000011553b50d WebCore`WebCore::EventHandler::mouseUp(WebEvent*) + 237
frame #22: 0x00000001150a2be3 WebKitLegacy`-[WebHTMLView mouseUp:] + 115
frame #23: 0x00000001160e230d WebCore`-[WAKView _selfHandleEvent:] + 125
frame #24: 0x00000001160e226f WebCore`-[WAKView handleEvent:] + 63
frame #25: 0x00000001160e546d WebCore`-[WAKWindow sendEventSynchronously:] + 269
frame #26: 0x0000000110e02e81 UIKit`__47-[UIWebDocumentView(Interaction) performClick:]_block_invoke + 740
frame #27: 0x0000000110e02b97 UIKit`-[UIWebDocumentView(Interaction) performClick:] + 68
frame #28: 0x0000000110e0349f UIKit`__64-[UIWebDocumentView(Interaction) _sendMouseMoveAndAttemptClick:]_block_invoke + 345
frame #29: 0x00000001160fd996 WebCore`HandleRunSource(void*) + 390
frame #30: 0x0000000110651301 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #31: 0x00000001106471a7 CoreFoundation`__CFRunLoopDoSources0 + 423
frame #32: 0x00000001106466e3 CoreFoundation`__CFRunLoopRun + 867
frame #33: 0x00000001106460f8 CoreFoundation`CFRunLoopRunSpecific + 488
frame #34: 0x00000001160fd325 WebCore`RunWebThread(void*) + 469
frame #35: 0x000000011286799d libsystem_pthread.dylib`_pthread_body + 131
frame #36: 0x000000011286791a libsystem_pthread.dylib`_pthread_start + 168
frame #37: 0x0000000112865351 libsystem_pthread.dylib`thread_start + 13