Giter Club home page Giter Club logo

squall's Introduction

squall

A squirrel C++11 binding

License: Modified BSD

To make VM but do nothing

C++

#include "squall/squall_vm.hpp"

int main() {
    squall::VM vm; // may throw squall::squirrel_error
    return 0;
}

To make VM with standard library but do nothing

C++

#include "squall/squall_vmstd.hpp"

int main() {
    squall::VMStd vm; // may throw squall::squirrel_error
    return 0;
}

To call squirrel function from C++

Squirrel

function foo() {
  print("==== foo0 called\n");
  return 0;
}

C++

#include "squall/squall_vmstd.hpp"
#include <iostream>

int main() {
    try {
        squall::VMStd vm;
        vm.dofile("test.nut");

        int n1 = vm.call<int>("foo", 7);
        std::cout << "**** return value: " << n1 << std::endl;
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
        return 1;
    }

    return 0;
}

To call C++ function from squirrel

Squirrel

function baz() {
  print("==== baz called\n");
  return bar(4649);
}

C++

#include "squall/squall_vmstd.hpp"
#include <iostream>

int main() {
    try {
        squall::VMStd vm;
        vm.dofile("test.nut");

        vm.defun("bar", [=](int x)->int {
                std::cout << "**** lambda: " << x << std::endl;
                return 7777;
            });

        int n2 = vm.call<int>("baz");
        std::cout << "**** return value: " << n2 << std::endl;
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
        return 1;
    }

    return 0;
}

To export C++ object and call member function

Squirrel

function zot(foo) {
  print("==== zot called\n");
  //print(foo);
  foo.bar();
  foo.bar();
  foo.bar();
  foo.bar();
  foo.bar();
  foo.bar();
  foo.baz("hello");
}

C++

#include "squall/squall_vmstd.hpp"
#include "squall/squall_klass.hpp"
#include <iostream>

class Foo {
public:
    Foo() : n_(0) {}
    int n_;

    void bar() {
        n_++;
        std::cerr << "**** bar called: " << n_ << std::endl;
    }
    int baz(const std::string& s) {
        std::cerr << "**** baz called: " << s << std::endl;
        return 4649;
    }
};

int main() {
    try {
        squall::VMStd vm;
        vm.dofile("klass.nut");

        squall::Klass<Foo> k(vm, "Foo");
        k.func("bar", &Foo::bar);
        k.func("baz", &Foo::baz);

        Foo foo;
        vm.call<void>("zot", &foo);
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
        return 1;
    }

    return 0;
}

To register raw global function

Squirrel

function bar() {
  foo();
}

C++

#include "squall/squall_vmstd.hpp"
#include <iostream>

SQInteger foo(HSQUIRRELVM vm) {
    std::cout << "**** foo called" << std::endl;
    return 0;
}

int main() {
    try {
        squall::VMStd vm;
        vm.dofile("rawfunc.nut");

        vm.defraw("foo", foo);
        vm.call<void>("bar");
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

To use coroutine

Squirrel

function foo() {
  print("==== foo called\n");
  local n = 4649;
  n = suspend(n);
  print("==== foo resumed\n");
  n = suspend(n);
  print("==== foo resumed\n");
  n = suspend(n);
  print("==== foo resumed\n");
  return n;
}

C++

#include "../squall/squall_vmstd.hpp"
#include <iostream>

void foo(int) {
}

int main() {
    try {
        squall::VMStd vm;
        vm.dofile("coroutine.nut");

        // You have better to make a scope because Coroutine dtor cleans up
        // Squirrel stack. co.result<T>() has the same effect, but if you
        // forget to call this, the stack might be completely broken.
        // The ownership of this clean-up behavior is transferred through
        // move semantics.
        {
            squall::Coroutine co = vm.co_call("foo");
            while (co.suspended()) {
                int n = co.yielded<int>();
                std::cerr << "**** foo yielded: " << n << std::endl;
                co.resume(n*2);
            }
            std::cerr << "**** foo result: " << co.result<int>() << std::endl;
        }
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

To import/export closure

Squirrel

function bar() {
  foo(@(s)s.len(), "hello world");
  return 7;
}

C++

#include "../squall/squall_vmstd.hpp"
#include <iostream>

void foo(std::function<int (const std::string&)> f, const std::string& s) {
    std::cout << "**** foo called: " << s << std::endl;
    std::cout << "**** closure return value: " << f(s) << std::endl;
}

int main() {
    try {
        squall::VMStd vm;
        vm.dofile("closure.nut");

        vm.defun("foo", &foo);
        int n = vm.call<int>("bar");
        std::cout << "**** return value: " << n << std::endl;
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

To derive

Squirrel

function zot(foo2) {
  foo2.bar2();
  foo2.bar();
}

C++

#include "../squall/squall_vmstd.hpp"
#include <iostream>

class Foo {
public:
    Foo() {}

    void bar() {
        std::cerr << "**** bar called: " << std::endl;
    }
};

class Foo2 : public Foo {
public:
    void bar2() {
        std::cerr << "**** bar2 called: " << std::endl;
    };
};

int main() {
    try {
        {
            squall::Klass<Foo> k(vm, "Foo");
            k.func("bar", &Foo::bar);
        }

        {
            squall::Klass<Foo2, Foo> k(vm, "Foo2");
            k.func("bar2", &Foo2::bar2);
        }

        Foo2 foo2;
        vm.call<void>("zot", &foo2);
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

To export member variables

Squirrel

function zot(foo) {
  print(foo.baz); print("\n");
  print(foo.zot); print("\n");
  foo.baz = 49;
  print(foo.baz); print("\n");
  // foo.zot = 34; // error, because zot is const member variable
  // print(foo.zot); print("\n");
}

C++

#include "../squall/squall_vmstd.hpp"
#include "../squall/squall_klass.hpp"
#include <iostream>

class Foo {
public:
    Foo() : zot(32) {}

    std::int32_t baz;
    const std::int32_t zot;
};

int main() {
    try {
        squall::VMStd vm;
        vm.dofile("var.nut");

        squall::Klass<Foo> k(vm, "Foo");
        k.var("baz", &Foo::baz);
        k.var("zot", &Foo::zot);
        
        Foo foo;
        foo.baz = 47;
        vm.call<void>("zot", &foo);
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

To define property

Squirrel

function baz(foo) {
  print(foo.bar1); print("\n");
  foo.bar2 = 42;
  print(foo.bar1); print("\n");
  print(foo.bar2); print("\n");
}

C++

#include "../squall/squall_vmstd.hpp"
#include "../squall/squall_klass.hpp"
#include <iostream>

class Foo {
public:
    Foo() { zot = 37; }

    int get_zot() const {
        return zot;
    }
    void set_zot(int n) {
        zot = n * 2;
    }
    
    int zot;
};


int main() {
    try {
        squall::VMStd vm;
        vm.dofile("prop.nut");

        squall::Klass<Foo> k(vm, "Foo");
        k.prop("bar1", &Foo::get_zot);
        k.prop("bar2", &Foo::get_zot, &Foo::set_zot);
        
        Foo foo;
        vm.call<void>("baz", &foo);
    }
    catch(squall::squirrel_error& e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

squall's People

Contributors

arnavb avatar jonigata avatar kuzawa avatar robloach avatar ryooooooga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

squall's Issues

std::bindした関数を直接渡せない

XCode 6.1.1

void func(int num)
{
    printf("%d", num);
}

std::function<void ()> a = std::bind(&func, 10);
_vm.defun("func", a); // ok

auto b = std::bind(&func, 10);
_vm.defun("func", b); // but compile erro

Make squall::Klass moveable

Basically titile. The class does not have any member that prevents it from being moved from, so deletion of move constructor/assignment seems unreasonable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.