Changes between Version 8 and Version 9 of SimulationSyntax


Ignore:
Timestamp:
2009-12-15 21:16:42 (4 years ago)
Author:
Philip
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SimulationSyntax

    v8 v9  
    236236== Allowing interfaces to be implemented in JS == 
    237237 
     238If we want to allow both C++ and JS implementations of `ICmpExample`, we need to define a special component type that proxies all the C++ methods to the script. Add the following to '''`ICmpExample.cpp`''': 
     239{{{ 
     240#!cpp 
     241#include "simulation2/scripting/ScriptComponent.h" 
     242 
    238243... 
    239244 
     245class CCmpExampleScripted : public ICmpExample 
     246{ 
     247public: 
     248    DEFAULT_SCRIPT_WRAPPER(ExampleScripted) 
     249 
     250    virtual int DoWhatever(int x, int y) 
     251    { 
     252        return m_Script.Call<int> ("DoWhatever", x, y); 
     253    } 
     254}; 
     255 
     256REGISTER_COMPONENT_SCRIPT_WRAPPER(IID_Example, ExampleScripted) 
     257}}} 
     258 
     259Then add to '''`TypeList.h`''': 
     260{{{ 
     261#!cpp 
     262COMPONENT(ExampleScripted) 
     263}}} 
     264 
     265`m_Script.Call` takes the return type as a template argument, then the name of the JS function to call and the list of parameters. You could do extra conversion work before calling the script, if necessary. You need to make sure the types are handled by `ToJSVal` and `FromJSVal` (as discussed before) as appropriate. 
     266 
    240267== Defining component types in JS == 
    241268