Commit 37417da2 authored by jhammen's avatar jhammen
Browse files

initial import extension source

parent fef2aefa
CMakeLists.txt.user
api/docs
cmake_minimum_required(VERSION 3.5)
project(bipscript-samplext LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
aux_source_directory("src" SRC_LIST)
aux_source_directory("api/bindings" SRC_LIST)
include_directories("src")
include_directories("xtapi")
include_directories("api/bindings")
add_library(${PROJECT_NAME} SHARED ${SRC_LIST})
# bipscript-samplext
Bipscript sample extension
\ No newline at end of file
Bipscript sample extension
Requires bipscript v0.16+
Build as a shared library using CMake libbipscript-samplext.so
Install: symlink to /usr/local/lib/bipscript/ext/SampleXt.bxt.so
Test via test.bip
<?xml version="1.0" encoding="UTF-8"?>
<package name="SampleXt">
<properties/>
<methods/>
<classes>
<class name="MyClass" include="myclass">
<ctors>
<ctor>
<parameters>
<parameter name="name" type="string">
</parameter>
</parameters>
</ctor>
</ctors>
<properties>
<property name="name" type="string" readonly="true"/>
</properties>
<methods>
<method name="favnum" returns="integer"/>
</methods>
</class>
</classes>
</package>
/*
* API bindings for xt package SampleXt
* this file was auto-generated by bapigen
*/
#include "bindsamplext.h"
#include "bipxtapi.h"
#include "bindutil.h"
#include "myclass.h"
#include <stdexcept>
#include <cstring>
namespace bapigen {
using namespace samplext;
// object references to types in this package
void * SampleXtMyClassObject;
//
// SampleXt.MyClass class
//
#define BIND_CLASS_NAME "MyClass"
#define BIND_METHOD_NAME "constructor"
BXTResult SampleXtMyClassCtor(int32_t nargs)
{
BIND_GET_NARGS()
MyClass *obj;
BIND_NARGS_CHECK_EXACT(1) // check exact parameter count
// get parameter 1 "name" as string
BIND_GET_CHECK_STRING(name, 1)
// call the implementation
BIND_WRAP_TRY(obj = new MyClass(name))
// return pointer to new object
BIND_PUT_THIS(obj, false)
return 1;
}
#undef BIND_METHOD_NAME
//
// SampleXt.MyClass property getters
//
#define BIND_METHOD_NAME "getter"
BXTResult SampleXtMyClassGetters(BXTIndex)
{
// get property name
const char* key;
bipxt_get_string(2, &key);
BIND_GET_THIS_PROPERTY(MyClass)
// dispatch appropriate getter
if(!strcmp(key, "name")) {
BIND_DECLARE_STRING ret = obj->name();
BIND_PUSH_STRING(ret)
return 1;
}
else {
return bipxt_not_found();
}
}
#undef BIND_METHOD_NAME
//
// SampleXt.MyClass property setters
//
#define BIND_METHOD_NAME "setter"
BXTResult SampleXtMyClassSetters(BXTIndex)
{
// get property name
const char* key;
bipxt_get_string(2, &key);
BIND_GET_THIS_PROPERTY(MyClass)
// dispatch appropriate setter
if(!strcmp(key, "name")) {
return bipxt_error("property \"name\" is read-only");
}
else {
return bipxt_not_found();
}
}
#undef BIND_METHOD_NAME
//
// SampleXt.MyClass favnum
//
#define BIND_METHOD_NAME "favnum"
BXTResult SampleXtMyClassfavnum(BXTIndex nargs)
{
BIND_GET_NARGS()
BIND_GET_THIS_INSTANCE(MyClass)
BIND_NARGS_CHECK_EXACT(0) // check exact parameter count
// return value
BIND_DECLARE_INTEGER ret;
// call the implementation
BIND_WRAP_TRY(ret = obj->favnum())
// push return value
BIND_PUSH_INTEGER(ret)
return 1;
}
#undef BIND_METHOD_NAME
BXTResult SampleXtMyClassToString(int32_t)
{
bipxt_put_string("(SampleXt.MyClass)", 18);
return 1;
}
#undef BIND_CLASS_NAME
void bindSampleXt()
{
// create class SampleXt.MyClass
SampleXtMyClassObject = bipxt_bind_class("MyClass", 7, &SampleXtMyClassCtor);
// bipxt_bind_method("_cloned", 7, BindUtil::Clone<MyClass>); // clone
bipxt_bind_method("_get", 4, &SampleXtMyClassGetters);
bipxt_bind_method("_set", 4, &SampleXtMyClassSetters);
bipxt_bind_method("favnum", 6, &SampleXtMyClassfavnum);
bipxt_bind_method("_tostring", 9, &SampleXtMyClassToString);
bipxt_bind_class_end();
}
}
\ No newline at end of file
#ifndef BINDSAMPLEXT_H
#define BINDSAMPLEXT_H
namespace bapigen
{
// object references to types in this package
extern void * SampleXtMyClassObject;
// method to bind this package
void bindSampleXt();
}
#endif // BINDSAMPLEXT_H
\ No newline at end of file
/*
MIT No Attribution
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include "bindsamplext.h"
#include "bipxtapi.h"
/*
* Sample bipscript extension
*
* these are the top level init/shutdown methods
*
*/
BXTResult bipxt_ext_init(uint32_t version) {
// TODO: actually check version
printf("Init sample extension\n");
return 0;
}
void bipxt_ext_init_pack(const char *name) {
printf("Init ext package: %s\n", name);
// TODO: check package name
bapigen::bindSampleXt();
}
void bipxt_ext_shutdown() {}
/*
MIT No Attribution
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef MYCLASS_H
#define MYCLASS_H
#include <stdlib.h>
#include <time.h>
namespace samplext {
class MyClass {
const char *mname;
public:
MyClass(const char *name) : mname(name) { srand(time(0)); }
const char *name() { return mname; }
int favnum() { return rand(); }
};
} // namespace samplext
#endif // MYCLASS_H
import SampleXt
function testMyClass() {
local obj = SampleXt.MyClass("Joe")
println("Hi my name is " + obj.name)
println("my favorite number is " + obj.favnum())
}
testMyClass()
/*
MIT No Attribution
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef BINDUTIL_H
#define BINDUTIL_H
#define BIND_GET_NARGS()
#define BIND_NARGS_CHECK_EXACT(Num) \
if (nargs != Num + 1) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " method expected exactly " #Num \
" parameters"); \
}
#define BIND_NARGS_CHECK_MIN(Min) \
if (nargs < Min + 1) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " method expected at least " #Min \
" parameters"); \
}
#define BIND_NARGS_CHECK_MAX(Max) \
if (nargs > Max + 1) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " method expected at most " #Max \
" parameters"); \
}
#define BIND_GET_THIS_INSTANCE(Class) \
void *userPtr = 0; \
if (BXT_FAILED(bipxt_get_this(&userPtr))) { \
return bipxt_error(BIND_CLASS_NAME " method needs an instance of " BIND_CLASS_NAME); \
} \
Class *obj = static_cast<Class *>(userPtr);
#define BIND_PUT_THIS(Name, Delete) bipxt_put_this(Name, Delete);
#define BIND_GET_THIS_PROPERTY(Class) \
void *userPtr = 0; \
if (BXT_FAILED(bipxt_get_object(1, &userPtr, 0))) { \
return bipxt_error("property needs an instance of " BIND_CLASS_NAME); \
} \
Class *obj = static_cast<Class *>(userPtr); \
if (!obj) { \
return bipxt_not_found(); \
}
#define BIND_DECLARE_BOOL BXTBoolean
#define BIND_GET_CHECK_BOOL(Name, Pos) \
BXTBoolean Name; \
if (BXT_FAILED(bipxt_get_boolean(Pos + 1, &Name))) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type boolean"); \
}
#define BIND_PUSH_BOOL(Name) bipxt_put_boolean(Name);
#define BIND_DECLARE_INTEGER BXTInteger
#define BIND_GET_CHECK_INTEGER(Name, Pos) \
BXTInteger Name; \
if (BXT_FAILED(bipxt_get_integer(Pos + 1, &Name))) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type integer"); \
}
#define BIND_PUSH_INTEGER(Name) bipxt_put_integer(Name);
#define BIND_DECLARE_FLOAT BXTFloat
#define BIND_GET_CHECK_FLOAT(Name, Pos) \
BXTFloat Name; \
if (BXT_FAILED(bipxt_get_float(Pos + 1, &Name))) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type float"); \
}
#define BIND_PUSH_FLOAT(Name) bipxt_put_float(Name);
#define BIND_DECLARE_STRING const char *
#define BIND_GET_CHECK_STRING(Name, Pos) \
const char *Name; \
if (BXT_FAILED(bipxt_get_string(Pos + 1, &Name))) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type string"); \
}
#define BIND_GET_CHECK_ENUMERATION(Name, Pos, Pack, Enum) \
BXTInteger int##Name; \
if (BXT_FAILED(bipxt_get_integer(Pos + 1, &int##Name))) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type " #Pack "." #Enum); \
} \
Pack::Enum Name = static_cast<Pack::Enum>(int##Name);
#define BIND_PUSH_STRING(Name) bipxt_put_string(Name, strlen(Name));
#define BIND_GET_CHECK_INTERFACE(Name, Pos, Pack, Class, QName) \
QName *Name = get##Pack##Class(Pos + 1); \
if (Name == 0) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type " #Pack "." #Class); \
}
#define BIND_GET_CHECK_OBJECT(Name, Pos, Pack, Class, QName) \
QName *Name = BindUtil::getObject<QName>(Pos + 1, Pack##Class##Object); \
if (Name == 0) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type " #Pack "." #Class); \
}
#define BIND_PUSH_OBJECT(Name, Type) bipxt_put_object(Name, Type);
#define BIND_GET_CHECK_INSTANCE(Name, Pos) \
BXTInstance Name; \
if (BXT_FAILED(bipxt_get_instance(Pos + 1, &Name))) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type instance"); \
}
#define BIND_GET_CHECK_FUNCTION(Name, Pos) \
BXTFunction Name; \
if (BXT_FAILED(bipxt_get_function(Pos + 1, &Name))) { \
return bipxt_error(BIND_CLASS_NAME " " BIND_METHOD_NAME " argument " #Pos " \"" #Name \
"\" is not of type function"); \
}
#define BIND_WRAP_TRY(Exp) \
try { \
Exp; \
} catch (std::exception const &e) { \
return bipxt_error(e.what()); \
}
class BindUtil {
public:
// templates
template <class T> static T *getObject(int index, BXTClass Object) {
void *objPtr;
if (!BXT_FAILED(bipxt_get_object(index, &objPtr, Object))) {
return static_cast<T *>(objPtr);
}
return nullptr;
}
};
#endif // BINDUTIL_H
/*
MIT No Attribution
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef BIPXTAPI_H
#define BIPXTAPI_H
#include <stdint.h>
#define BXT_FAILED(result) (result < 0)
extern "C" {
typedef uint32_t BXTBoolean;
#ifdef _LP64
typedef long long BXTInteger;
#else
typedef int BXTInteger;
#endif
typedef float BXTFloat;
typedef int32_t BXTResult;
typedef int32_t BXTIndex;
typedef BXTResult (*BXTMethod)(BXTIndex);
typedef void *BXTClass;
typedef void *BXTFunction;
typedef void *BXTInstance;
BXTResult bipxt_ext_init(uint32_t version);
void bipxt_ext_init_pack(const char *name);
void bipxt_ext_shutdown();
BXTClass bipxt_bind_class(const char *name, uint32_t namelen, BXTMethod ctor);
void bipxt_bind_method(const char *name, uint32_t namelen, BXTMethod func);
void bipxt_bind_class_end();
void bipxt_bind_enumeration(const char *name);
void bipxt_bind_enumerator(const char *name, int32_t value);
void bipxt_bind_enumeration_end();
BXTResult bipxt_get_boolean(BXTIndex index, BXTBoolean *boolp);
void bipxt_put_boolean(BXTBoolean value);
BXTResult bipxt_get_integer(BXTIndex index, BXTInteger *intp);
void bipxt_put_integer(BXTInteger value);
BXTResult bipxt_get_float(BXTIndex index, BXTFloat *floatp);
void bipxt_put_float(BXTFloat value);
BXTResult bipxt_get_string(BXTIndex index, const char **strp);
void bipxt_put_string(const char *str, uint32_t strlen);
BXTResult bipxt_get_object(BXTIndex index, void **ptr, BXTClass type);
void bipxt_put_object(void *obj, void *type);
BXTResult bipxt_get_this(void **ptr);
void bipxt_put_this(const void *obj, uint32_t rdelete);
BXTResult bipxt_get_instance(BXTIndex index, BXTInstance *ptr);
BXTResult bipxt_get_function(BXTIndex index, BXTFunction *ptr);
void bipxt_add_reference(BXTInstance object);
BXTResult bipxt_execute(BXTFunction function, BXTInstance context);
void bipxt_event_handler(BXTMethod handler);
BXTResult bipxt_error(const char *mesg);
BXTResult bipxt_not_found();
}
#endif // BIPXTAPI_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment