Calling Scripting functions from C++

We now have a fairly good grasp on exposing C++ functions and objects to CliPP (and the Javascript engine) In some cases we might want to go the other way, calling for example javascript functions from C++. This is possible. Let's examine how:

    context* c=parser.get_context();

    //Create a new number object, using the Number constructor from the javascript language
    valueP result = c->global()["Number"](86.97);

    //Convert the number to a hexadecimal digit
    valueP hex    = result["toString"](16);

    //Convert the javascript string to const char*
    const char* text = unwrap<const char*>(hex)();  

Calling a Javascript function from C++

If you write a function in javascript, it is easy to call it from C++:

      //Create a function in Javascript	
      parser.parse(
	"function faculty(input) {\n"
      "   if(input<=1) return 1;\n"
      "   return input*faculty(input-1);\n"
      "}");
    
    context* c=parser.get_context();

    //Call function from C++    
    valueP result = c->global()["faculty"](10);

    //Extract answer (should be 3628800)
    int output = unwrap<int>(result)();
  

Shortcomings

Here is a list of all the aspects of using scripting functionallity from inside C++

This is a list of all the functionality that is complete and working.

Supported functionality
constructors valueP result = c->global()["Number"](86.79);
objects The result of the above expression
functions result["toString"](16);
read properties valueP pinf = result["POSITIVE_INFINITY"];

Here are some things that will currently not work. Both of the below deficiencies arise because it is hard to implement non-intrusive general templated operator overloadings.

Unsupported functionality
write properties result1["x"] = 8;
operators result2 = result1["x"] + 98.1;

You can still tweak this to make it work by applying the workarounds below:

Workarounds
write properties as functions result1["x"](8);
operators using wrapping result2 = invoke_binary_operator<'+'>(result1["x"],wrap(98.1,c));

Use especially the operator wrapping with care, as it may be replaced with a better mechanism in the future.