[Sciter] Script 와 Native 의 상호작용

원본 주소:http://www.cnblogs.com/yinxufeng/p/5653920.html
Script 와 Native 의 상호작용 기초
방식 을 간소화 하 다
Script 호출 Native
Native 호출 스 크 립 트
Script 와 Native 의 상호작용 기초
1 SDK 의 "plain - win" 예 를 참조 하여 창 클래스 를 사용자 정의 합 니 다. sciter: host < window > 와 sciter:: event 계승 합 니 다.handler

  
  
  
  
  1. class window: public sciter::host<window>
  2. , public sciter::event_handler
  3. {
  4. HWND _hwnd;
  5. static LRESULT CALLBACK wnd_proc(HWND, UINT, WPARAM, LPARAM);
  6. static window* ptr(HWND hwnd);
  7. static bool init_class();
  8. public:
  9. // notification_handler traits:
  10. HWND get_hwnd() const { return _hwnd; }
  11. HINSTANCE get_resource_instance() const{ return ghInstance; }
  12. window();
  13. bool init(); // instance
  14. bool is_valid() const { return _hwnd != 0; }
  15. };


HWND get_hwnd() const { return _hwnd; }
HINSTANCE get_resource_instance() const{ return ghInstance; }
, host

2 , sciter::attach_dom_event_handler Sciter DOM


  
  
  
  
  1. bool window::init()
  2. {
  3. SetWindowLongPtr(_hwnd, GWLP_USERDATA, LONG_PTR(this));
  4. setup_callback();
  5. // // attach event_handler to the window
  6. sciter::attach_dom_event_handler(_hwnd, this);
  7. load_file(L"res:default.htm");
  8. return true;
  9. }

SDK “ ”

  • sciter-sdk/include/sciter-win-main.cpp - on Windows
  • sciter-sdk/include/sciter-osx-main.mm - on OS X
  • sciter-sdk/include/sciter-gtk-main.cpp - on Linux

sciter-win-main.cpp , , sciter::window

Script Native

, MFC


  
  
  
  
  1. BEGIN_FUNCTION_MAP
  2. FUNCTION_0("helloWorld", helloWorld);
  3. FUNCTION_2("native_sum", native_sum); // 2 2
  4. FUNCTION_0("native_api", native_api);
  5. END_FUNCTION_MAP
  6. sciter::string helloWorld() { return L"Hello u-minimal World"; }
  7. int native_sum(sciter::value a, sciter::value b) { return a.d + b.d; }
  8. sciter::value native_api() {
  9. sciter::value api_map;
  10. sciter::value api_math_map;
  11. std::function<int(int,int)> native_sum = [](int a, int b) { return a + b; };
  12. std::function<int(int,int)> native_sub = [](int a, int b) { return a - b; };
  13. api_math_map.set_item(sciter::value("sum"), sciter::vfunc( native_sum ));
  14. api_math_map.set_item(sciter::value("sub"), sciter::vfunc( native_sub ));
  15. api_map.set_item(sciter::value("math"), api_math_map);
  16. return api_map;
  17. }

tiscript


  
  
  
  
  1. <script type="text/tiscript">
  2. var message = view.helloWorld();
  3. view.native_sum(a, b);
  4. view.nativeApi().math.sub(c, d);
  5. </script>

view , Sciter , , close( ), msgbox( ),selectFile( ),Update 。

FUNCTION_MAP , view :view.native_sum(a, b)。

Native , native_api,view , view.native_api().math.xxx

sciter::value native_api()Mapsciter::value, :

return {
  math: {
    sum: {native_sum},
    sub: {native_sub},
  }
}

Native Script

script :


  
  
  
  
  1. <script type="text/tiscript">
  2. namespace Test {
  3. function add(n1,n2) { return n1+n2; }
  4. }
  5. </script>

Native :


  
  
  
  
  1. sciter::dom::element root = self->get_root();
  2. sciter::value r;
  3. try {
  4. r = root.call_function("Test.add",sciter::value(2),sciter::value(2));
  5. } catch (sciter::script_error& err) {
  6. std::cerr << err.what() << std::endl;
  7. }
  8. // or sciter::value r = self->call_function("Test.add",sciter::value(2),sciter::value(2));
  9. assert(r.is_int() && r.get(0) == 4);

Test script
self sciter::host< window >

좋은 웹페이지 즐겨찾기