I know that some people simply load the JS libraries in a browser widget and call from there.
The issue is calling/driving it from LCScript and passing data to/from it.
I can say that 'do as javascript' is the recommended way that I've seen, not sure why you consider this 'bad practice'?
Using "eval" for interop is bad programming practice, like passing user-generated format strings to printf or using Egyptian hieroglyphics in variable names. I'd link explanations but URLs are banned, so google "why is eval bad practice". These examples are in other languages but the reasoning is universal.
If it doesn't work, it might simply be an issue with your code, rather than an inability to use a JS library...
From the LiveCode documentation for "do":
In HTML5 applications, "JavaScript" is the only supported alternateLanguage. The result is set to the value returned by executing the statementList, as long as the value is a scalar (a string, number, boolean or undefined). Returning JavaScript arrays or objects is not supported.
That's a crippling limitation, not an issue with my code. First, this means a JS library with 100 non-OO functions must have each function wrapped with string-serialization, like
Code: Select all
function foo001(arg1)
do "JSON->stringify(foo001(" & JsonExport(arg1) & "," & JsonExport(arg2) & "))" as "javascript"
return JsonImport(result())
end foo001
function foo002(arg1, arg2)
do "JSON->stringify(foo002(" & JsonExport(arg1) & "," & JsonExport(arg2) & "))" as "javascript"
return JsonImport(result())
end foo002
function foo003(arg1, arg2, arg3)
do "JSON->stringify(foo003(" & JsonExport(arg1) & "," & JsonExport(arg2) & "," & JsonExport(arg3) & "))" as "javascript"
return JsonImport(result())
end foo003
etc... -- (note I can't type JSON-dot-stringify due to URL ban)
Second, functions that require instantiating objects and maintaining state between calls are impossible to wrap with LC, as JSON serialization destroys the object.
And every upstream change to the JS lib (which is updated frequently) requires manually fixing these bindings.
I am however curious as to what is so 'monstrous' that LC cannot manage and JS can?
There isn't much that cannot be done more easily with LC... so genuinely curious.
Take my previous example, or extend it: mapping these functions over data structures is painful in LC. In JS it was just:
Code: Select all
const exOutputs = exInputs->map(foo002)
But LC script has no functional or lambda syntax, and requires:
Code: Select all
local exOutputs
repeat with i = 1 to (number of elements of exInputs)
put foo002(exInputs[i], i) into exOutputs[i]
end repeat
The more sophisticated the example, the greater the disparity will be.