About six years ago I used Real Studio to create the application Newtons Method. This used RBScript to allow the user to enter a function f(x) and find x for which f(x) = 0. But I wanted the variables to be the BigFloats provided by my fp Plugin, so the calculation could be carried out at any chosen precision. Even though the running RBScript is sandboxed, so cant directly see or use BigFloats, I was able to make it do so.
Below is a simple project, BigFloatInScript, showing how I implemented, now using BigFloats in XojoScript, not RBScript. Perhaps there are simpler ways. If so, I would like to know them. Open the project and run it. In the Script TextArea put the code
Dim x As BigFloat
x = 2
x = sqrt(x)
output = str(x)
Click Go and see in the Out TextArea:
1.414213562373095048801688724209698078569671875376948073176679738
which is the square root of 2 correct to 64 digits.
How was this done? Basically the idea is to use a Dictionary as a buffer between a XojoScript and the outside Xojo world. BigFloats are stored in the Dictionary by the outside world and their IDs are used by XojoScript to do the job. But the user script looks like normal Xojo code.
In the project look at the Go button code. Note there are two XojoScripts, BFScript, which runs and does the job, and myScriptContext, which is the context for BFScript. So examine TextAreaPrefix.text and look at the methods and properties of BFScriptContext.
[quote=320363:@Robert Delaney]About six years ago I used Real Studio to create the application Newtons Method. This used RBScript to allow the user to enter a function f(x) and find x for which f(x) = 0. But I wanted the variables to be the BigFloats provided by my fp Plugin, so the calculation could be carried out at any chosen precision. Even though the running RBScript is sandboxed, so cant directly see or use BigFloats, I was able to make it do so.
Below is a simple project, BigFloatInScript, showing how I implemented, now using BigFloats in XojoScript, not RBScript. Perhaps there are simpler ways. If so, I would like to know them. Open the project and run it. In the Script TextArea put the code
Dim x As BigFloat
x = 2
x = sqrt(x)
output = str(x)
Click Go and see in the Out TextArea:
1.414213562373095048801688724209698078569671875376948073176679738
which is the square root of 2 correct to 64 digits.
How was this done? Basically the idea is to use a Dictionary as a buffer between a XojoScript and the outside Xojo world. BigFloats are stored in the Dictionary by the outside world and their IDs are used by XojoScript to do the job. But the user script looks like normal Xojo code.
In the project look at the Go button code. Note there are two XojoScripts, BFScript, which runs and does the job, and myScriptContext, which is the context for BFScript. So examine TextAreaPrefix.text and look at the methods and properties of BFScriptContext.
Bob[/quote]
Bob, FWIW, what you’re doing is pretty much what you’d need to do… that is, create a class in XojoScript code which creates and interfaces with objects in your context object.
I would like to point out two things:
Your BFScriptContext object does not need to be a subclass of XojoScript.
The code that you have in TextAreaPrefix could just as easily be in a constant as part of the BFScriptContext object to keep everything in one place.
I think my idea of using a Dictionary as a buffer beweern a XojoScript and the Xojo outside world can be generalized. When a script like
Dim x As BigFloat
x = 2
is run, x does not point to a BigFloat. It points to a Dictionary entry which in turn points to the BigFloat which contains 2. Thats what can be generalizrd. The Dictionary entry can be a pointer to any object a programmer creates. The Dictionary just puts another pointer in the chain. The class then must be designed to translate operators with Dictionary pointers to objects into operator actions with the actual objects.
Thats why I posted my example project. I saw this generalization and thought other Xojo programmers might find it useful.
Thanks for posting this method. In the past, I’ve avoided using Xojoscript for certain tasks, because of the context limitations. You’ve come up with a very elegant solution.
Thanks also for the BigFloat plug-in. I’ve made very good use of it.