Can I break up the xojoscript to several parts?

class1 inheris from xojoscript

script = new class1
script.source=" function test() as integer
return 0 " //part one
script.run

script.source=" dim testvalue as integer=test() " //part two
script.run

you can access stuff via context property/class.

Than you can in your script decide what to do based on properties in context object.
This way you can run the same compiled code again and again on different parameters.

sorry ,but not very understand how to do?
do you mean my “script” object could change the context dynamic?

my orignal requirement is:

suppose I dim a function in my first script. and how to call it in my second script in the same script obj.

thanks.

You can’t directly call one script from the other. What Christian is proposing is that your second script calls a context method which runs the first script then returns the result back to the second script.

But looking at your original example code I see a misunderstanding.

script = new class1                              //create a script instance
script.source=" function test() as integer       //set it's source
return 0 " //part one
script.run                                       //execute it 
                                                 //execution of that source has finished
script.source="dim testvalue as integer=test()"  //set a different source
script.run                                       //execute this source

A XojoScript instance has a single source. When the source is replaced the previous source is gone. You never actually have 2 scripts there. It’s 1 script that’s having it’s source switched.

Maybe what you want to do is combine the 2 scripts into one.

script = new class1 script.source = _ "function test() As integer" + EndOfLine + _ " return 0" + EndOfLine + _ "end function" + EndOfLine + _ "dim testvalue as integer=test()" script.run

[quote=284222:@Will Shank]You can’t directly call one script from the other. What Christian is proposing is that your second script calls a context method which runs the first script then returns the result back to the second script.

But looking at your original example code I see a misunderstanding.

script = new class1                              //create a script instance
script.source=" function test() as integer       //set it's source
return 0 " //part one
script.run                                       //execute it 
                                                 //execution of that source has finished
script.source="dim testvalue as integer=test()"  //set a different source
script.run                                       //execute this source

A XojoScript instance has a single source. When the source is replaced the previous source is gone. You never actually have 2 scripts there. It’s 1 script that’s having it’s source switched.

Maybe what you want to do is combine the 2 scripts into one.

script = new class1 script.source = _ "function test() As integer" + EndOfLine + _ " return 0" + EndOfLine + _ "end function" + EndOfLine + _ "dim testvalue as integer=test()" script.run[/quote]

thanks shank ,
what I want to do is as you said, I have lots of small function dynamic define, which is not a “context” method.

the second way you provide is good as I am willing to do that. I just think of a better way.
as you know. when the script is long,and define many functions. it will waste time to execute it.
(my origin requirement is break them into several parts and execute seperately)

combine 2 script into one is good,I will precompile then before run it. (thougn I haven’t test it yet)

A long script doesn’t take any longer to execute, it just runs the code. It’s only the compiling that will take longer. XojoScript isn’t interpreted, it’s compiled, so once all those functions are compiled they don’t need to be processed again for a second Run().

But if you have a really long script that is taking significant time to compile, and you’re compiling often but with only a small change to one function, then it makes sense to do what Christian said. This will only work easily if the functions get and return simple scalar values (anything you can pass to a Context method: integer, string, boolean,etc). If you need to pass or return arrays or custom objects from the script code then it’s gets complicated.

ok?thanks?