Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
api.cpp
Go to the documentation of this file.
1#include "ffi.hpp"
2#include <PhasorFFI.h>
3
4namespace Phasor
5{
6
15{
16 switch (c_value.type)
17 {
19 return Phasor::Value();
21 return Phasor::Value(c_value.as.b);
22 case PHASOR_TYPE_INT:
23 return Phasor::Value(c_value.as.i);
25 return Phasor::Value(c_value.as.f);
27 if (c_value.as.s)
28 {
29 return Phasor::Value(c_value.as.s);
30 }
31 return Phasor::Value("");
32 case PHASOR_TYPE_ARRAY: {
33 std::vector<Phasor::Value> cpp_elements;
34 if (c_value.as.a.elements && c_value.as.a.count > 0)
35 {
36 cpp_elements.reserve(c_value.as.a.count);
37 for (size_t i = 0; i < c_value.as.a.count; ++i)
38 {
39 cpp_elements.push_back(from_c_value(c_value.as.a.elements[i]));
40 }
41 }
42 return Phasor::Value::createArray(std::move(cpp_elements));
43 }
44 default:
45 return Phasor::Value();
46 }
47}
48
57PhasorValue to_c_value(const Phasor::Value &cpp_value, std::vector<std::unique_ptr<char[]>> &string_arena,
58 std::vector<std::unique_ptr<PhasorValue[]>> &array_arena)
59{
60 switch (cpp_value.getType())
61 {
62 case ValueType::Null:
63 return phasor_make_null();
64 case ValueType::Bool:
65 return phasor_make_bool(cpp_value.asBool());
66 case ValueType::Int:
67 return phasor_make_int(cpp_value.asInt());
69 return phasor_make_float(cpp_value.asFloat());
70 case ValueType::String: {
71 const auto &str = cpp_value.asString();
72 auto c_str = std::make_unique<char[]>(str.length() + 1);
73 std::copy(str.begin(), str.end(), c_str.get());
74 c_str[str.length()] = '\0';
75 PhasorValue val = phasor_make_string(c_str.get());
76 string_arena.push_back(std::move(c_str));
77 return val;
78 }
79 case ValueType::Array: {
80 const auto &cpp_array = *cpp_value.asArray();
81 size_t count = cpp_array.size();
82 if (count == 0)
83 {
84 return phasor_make_array(nullptr, 0);
85 }
86
87 auto c_array = std::make_unique<PhasorValue[]>(count);
88 for (size_t i = 0; i < count; ++i)
89 {
90 c_array[i] = to_c_value(cpp_array[i], string_arena, array_arena);
91 }
92
93 PhasorValue val = phasor_make_array(c_array.get(), count);
94 array_arena.push_back(std::move(c_array));
95 return val;
96 }
97 default:
98 return phasor_make_null();
99 }
100}
101
102Phasor::Value c_native_func_wrapper(PhasorNativeFunction c_func, Phasor::VM *vm, const std::vector<Phasor::Value> &args)
103{
104 std::vector<std::unique_ptr<char[]>> string_arena;
105 std::vector<std::unique_ptr<PhasorValue[]>> array_arena;
106
107 std::vector<PhasorValue> c_args;
108 c_args.reserve(args.size());
109 for (const auto &arg : args)
110 {
111 c_args.push_back(to_c_value(arg, string_arena, array_arena));
112 }
113
114 PhasorValue c_result = c_func(reinterpret_cast<PhasorVM *>(vm), (int)c_args.size(), c_args.data());
115
116 return from_c_value(c_result);
117}
118
119void register_native_c_func(PhasorVM *vm, const char *name, PhasorNativeFunction func)
120{
121 Phasor::VM *cpp_vm = reinterpret_cast<Phasor::VM *>(vm);
122
123 auto wrapper = [func](const std::vector<Phasor::Value> &args, Phasor::VM *vm_param) -> Phasor::Value {
124 return c_native_func_wrapper(func, vm_param, args);
125 };
126
127 cpp_vm->registerNativeFunction(name, wrapper);
128}
129
130} // namespace Phasor
PhasorValue(* PhasorNativeFunction)(PhasorVM *vm, int argc, const PhasorValue *argv)
Signature for a native C function that can be registered with the Phasor VM.
Definition PhasorFFI.h:189
static PhasorValue phasor_make_array(const PhasorValue *elements, size_t count)
Definition PhasorFFI.h:124
static PhasorValue phasor_make_float(double f)
Definition PhasorFFI.h:108
static PhasorValue phasor_make_string(const char *s)
Definition PhasorFFI.h:116
static PhasorValue phasor_make_null()
Definition PhasorFFI.h:85
static PhasorValue phasor_make_bool(bool b)
Definition PhasorFFI.h:92
static PhasorValue phasor_make_int(int64_t i)
Definition PhasorFFI.h:100
struct PhasorVM PhasorVM
Phasor Virtual Machine pointer.
Definition PhasorFFI.h:44
@ PHASOR_TYPE_FLOAT
Definition PhasorFFI.h:52
@ PHASOR_TYPE_NULL
Definition PhasorFFI.h:49
@ PHASOR_TYPE_ARRAY
Definition PhasorFFI.h:54
@ PHASOR_TYPE_STRING
Definition PhasorFFI.h:53
@ PHASOR_TYPE_BOOL
Definition PhasorFFI.h:50
@ PHASOR_TYPE_INT
Definition PhasorFFI.h:51
Virtual Machine.
Definition VM.hpp:30
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
A value in the Phasor VM.
Definition Value.hpp:67
double asFloat() const
Get the value as a double.
Definition Value.hpp:214
std::string asString() const
Get the value as a string.
Definition Value.hpp:223
std::shared_ptr< ArrayInstance > asArray()
Get the value as an array.
Definition Value.hpp:230
int64_t asInt() const
Get the value as an integer.
Definition Value.hpp:205
ValueType getType() const
Get the type of the value.
Definition Value.hpp:121
bool asBool() const
Get the value as a boolean.
Definition Value.hpp:200
static Value createArray(std::vector< Value > elements={})
Definition Value.hpp:480
The Phasor Programming Language and Runtime.
Definition AST.hpp:11
void register_native_c_func(PhasorVM *vm, const char *name, PhasorNativeFunction func)
The concrete implementation of the PhasorRegisterFunction API call.
Definition api.cpp:119
Phasor::Value from_c_value(const PhasorValue &c_value)
Converts a C-style FFI value to a C++ VM value.
Definition api.cpp:14
Phasor::Value c_native_func_wrapper(PhasorNativeFunction c_func, Phasor::VM *vm, const std::vector< Phasor::Value > &args)
The "trampoline" that wraps a C function from a plugin.
Definition api.cpp:102
PhasorValue to_c_value(const Phasor::Value &cpp_value, std::vector< std::unique_ptr< char[]> > &string_arena, std::vector< std::unique_ptr< PhasorValue[]> > &array_arena)
Converts a C++ VM value to a C-style FFI value.
Definition api.cpp:57
Represents a value in the Phasor VM.
Definition PhasorFFI.h:64
struct PhasorValue::@257106260015165034006071221344325333366100147341::@324103060207145140324203224012174236216253240107 a
const PhasorValue * elements
Definition PhasorFFI.h:74
const char * s
Definition PhasorFFI.h:70
int64_t i
Definition PhasorFFI.h:68
PhasorValueType type
Definition PhasorFFI.h:65
union PhasorValue::@257106260015165034006071221344325333366100147341 as
size_t count
Definition PhasorFFI.h:75
double f
Definition PhasorFFI.h:69