Game Development Community

Compiling just one line of code.

by Ricky Taylor · in Torque Game Builder · 11/06/2005 (12:12 pm) · 13 replies

Yeah... Well...

Is it posible to compile a line of code into a char array, then execute it.

Thanks in advanced.

Ricky,

#1
11/06/2005 (12:14 pm)
Yes is it
#2
11/06/2005 (1:33 pm)
-_-. Thanks, I can't find the function to do so.

I can happily execute and compile one line at once.

But I want to have the string, and then execute it somewhere else.

How can I do this?

P.S. Posting almost one word answers, unless absolutely necessary, is usually called 'spamming' or 'boosting'.
#3
11/06/2005 (2:09 pm)
Are you looking for something like the eval() command? You can do something like this with it:

%code = "echo(\"Testing\");";
eval(%code);
#4
11/06/2005 (2:23 pm)
Nope, I want to be able to have the compiled code in a variable... And I want to do it in C++...

Pesudo Example:

char *code = "echo(\"hey\");";
char *out;

compile(code, out);

...

exec(out);
#5
11/06/2005 (2:31 pm)
Con::evaluatef()
#6
11/06/2005 (2:50 pm)
Ricky, if you are strictly talking about Torque functionality, Tom's suggestion of EvalueateF should work.

Otherwise, I belive the most equivlant thing to what you are looking for is called a function pointer. This is the same concept as Delegates in C#.
#7
11/06/2005 (2:57 pm)
I need to compile SEPARATELY from execution, theres a good reason, honest! =)

I want the equivalent of a DSO in a U8 *.

And later I want to be able to execute it.

I seem bad at explaining this. =/

Ricky,
#8
11/06/2005 (6:46 pm)
You can do this, set a breakpoint on the exec() (C++ side) ConsoleFunction and step through what it does... you'll see they load up the file into a char* . just skip the loading step and there u go.
#9
11/08/2005 (12:55 am)
Ricky,

To compile a file, you can use "compile(fileName)" from the scripts. As an example, I created a file which just had an "echo()" statement in it and then issued the previous "compile()" on it. This produced a file "test.cs.dso" file.

Here's something I knocked up for you. Using this code, you can then load it up and execute a DSO. Obviously you can store the stream in your executable or do whatever you want with it. You could even retrieve it from another server via a stream if you wanted, the sky's the limit. There are all sorts of ways to get into problems doing this but I guess I'll let you find that out yourself. ;)

// Must include at least these.
#include "console/console.h"
#include "console/ast.h"
#include "core/resManager.h"
#include "console/compiler.h"

// Test Execute a DSO.
ConsoleFunction( executeDSO, bool, 2, 2, "Test Execute a DSO" )
{
   Stream *compiledStream;

   // Read DSO File.
   compiledStream = ResourceManager->openStream(argv[1]);

   // Check for failure.
   if ( !compiledStream )
   {
       Con::warnf("Couldn't open stream!");
       return false;
   }

   // Read Version.
   U32 version;
   compiledStream->read(&version);
   // Correct Version?
   if(version != Con::DSOVersion)
   {
       // No!
       Con::warnf("Incorrect DSO Version!");
       ResourceManager->closeStream(compiledStream);
       return false;
   }

    // Create a code block.
    CodeBlock *pCode = new CodeBlock;
    // Read the stream into the code-block.
    pCode->read(argv[1], *compiledStream);
    // Close the stream.
    ResourceManager->closeStream(compiledStream);

    // Execute the sucker.
    pCode->exec(0, NULL, NULL, 0, NULL, false, NULL);

    // Done and dusted.
    return true;
}

... from scripts ...

executeDSO( "t2d/client/test.cs.dso" );

Hope this helps,

- Melv.
#10
11/08/2005 (7:43 am)
Damn! You've foiled my schemes!

I mean -- Of course im not gonna send it over the network... =)

Thanks loads... I hope melv is being paid in millions... ^_^

I must've missed that CodeBlock->read function...
#11
12/17/2005 (7:34 am)
Im guessing that compiled code wont run in another instance of T2D, (running a different game).

Because it seems to dislike me botching the code over the network and executing it there, although the network works and I can compile/exec locally.

Any ideas? Its not too crucial as I've found a way of encrypting my packets... But hey.

Thanks in advanced,
Ricky,
#12
12/17/2005 (3:40 pm)
Ricky, look at what the Exec() function does.

it loads a file up and then calls a function passing in the loaded script. (sorry not near my dev machine so you have to look at it yourself to find the function name)

with that function, you can pass in any text and it will be executed. So, sending stuff over the network, you dont need to compile it. just send over some plain-text (encrypt it first if i were you) and then run.
#13
12/20/2005 (7:51 am)
Thats what eval() does!

And yeah, thats what I had before this topic started, I didn't have the "encryption" though =D... Thanks for the hint....

Also, I have MD5Hash()! W00t =D.

The compilation code is soooo confusing!

=D

Ricky,