Discord
Login
Community
DARK THEME

Is it possible to call code from a string?

Now hear me out. I have seen a function in javascript that makes a string usable code. It looks about this way:

code = new Function(string); code();

Now, how do I something similar in Microscript? It is just a technical question.

No there is no such possibility in microScript.

Just one thing comes to mind, you can execute JavaScript code from microScript, with system.javascript(string); thus you could us this to run JavaScript code passed as a string.

Modern Problems require smart solutions.

Probably not what you expect but something like this works. MicroScript2.0

  returnFunc =  function( num_args )
    local fun = 0
    if num_args <= 0 then 
      fun = function()
           local entryTime = now()
// code 
           return result
      end
      return fun
    elsif num_args == 1 then 
      fun = function( arg1 )
          local entryTime = now()
//cod
          return result
      end
      return fun
    elsif num_args == 2 then 
      fun = function( arg1, arg2 )
          local entryTime = now()
//cod
          return result
      end
   end
   return fun
end

f0 = returnFunc(0)
f1 = returnFunc(1)
f2 = returnFunc(2)

print( f0() )
print( f1( 100 ))
print( f2( 100, 200 ))
 myObject = new MyClass()
 myObject.newFunc4 = returnFunc(4)
 myObject.newFunc4( 100, 200, 300, 400 )

I tried to make a code that shows how MicroScript sees a given function (bytecode) - it turned out poorly, but thanks to this I understand better how MicroScript2.0 works. Theoretically, it would be possible to inject the code, but not as a string, but as a bytecode.

With returnFunc you can create a new function and for example add it to an object.

I did something that I remember the original name of the function in the object, in its place I put the function received from returnFunc . The function I got from returnFunc refers to the original object function. In this way I wanted to debug the project or count the number of method calls.

Since you don't have to change anything in the object being debugged, it seems very attractive. It's kind of like a helper.

Well say no more, since I am working on a Mini IDE (In short M-IDE), that allows special code to be converted into a list of instructions, which will be executed. I am about 45-55% done.

Here is the source code with which you can inject JavaScript code into the MicroScript engine. https://microstudio.io/i/Loginus/inject2/

I was thinking about translating the code from MicroScript 2.0 to JavaScript - but it's probably beyond me.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community