I am playing around with the JavaScriptCore framework and have discovered two different ways to create booleans in a JavaScript context:
import JavaScriptCore
let context = JSContext()
let trueVal = JSValue(bool: true, in: context)
JSValueIsBoolean(context?.jsGlobalContextRef, trueVal?.jsValueRef) // true
let otherTrueValue = JSValueMakeBoolean(context?.jsGlobalContextRef, true)
JSValueIsBoolean(context?.jsGlobalContextRef, otherTrueValue) // true
What is the difference between JSValue(bool:in:)
and JSValueMakeBoolean
?
The generated header for JSValueRef
keeps this comment:
* Copyright (C) 2006 Apple Inc. All rights reserved.
And for JSValue
:
* Copyright (C) 2013 Apple Inc. All rights reserved.
Seems JavaScriptCore framework was first developed for an old OS X (showing 10.5+ in the reference page) with C-function based APIs. And later it has come to iOS with a modern class based APIs.
(Though, I have never used JavaScriptCore in such old OS X.)
I haven't explored JavaScriptCore so deeply inside it, so I'm not sure such C-function based APIs still needed in some cases or not.
But usually, you have no need to touch C-function APIs. For example, you can use isBoolean
property rather than C-function JSValueIsBoolean
.
if let trueVal = JSValue(bool: true, in: context) {
print(trueVal.isBoolean) //->true
}