Phasor 3.3.0
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
4#ifndef CMAKE_PCH
5#include "../VM/VM.hpp"
6#endif
7
8namespace Phasor
9{
10
19{
20 switch (c_value.type)
21 {
23 return Phasor::Value();
25 return Phasor::Value(c_value.as.b);
26 case PHASOR_TYPE_INT:
27 return Phasor::Value(c_value.as.i);
29 return Phasor::Value(c_value.as.f);
31 if (c_value.as.s)
32 {
33 return Phasor::Value(c_value.as.s);
34 }
35 return Phasor::Value("");
36 case PHASOR_TYPE_ARRAY: {
37 std::vector<Phasor::Value> cpp_elements;
38 if (c_value.as.a.elements && c_value.as.a.count > 0)
39 {
40 cpp_elements.reserve(c_value.as.a.count);
41 for (size_t i = 0; i < c_value.as.a.count; ++i)
42 {
43 cpp_elements.push_back(from_c_value(c_value.as.a.elements[i]));
44 }
45 }
46 return Phasor::Value::createArray(std::move(cpp_elements));
47 }
48 default:
49 return Phasor::Value();
50 }
51}
52
62PhasorValue to_c_value(const Phasor::Value &cpp_value, std::vector<std::unique_ptr<char[]>> &string_arena,
63 std::vector<std::unique_ptr<PhasorValue[]>> &array_arena)
64{
65 switch (cpp_value.getType())
66 {
67 case ValueType::Null:
68 return phasor_make_null();
69 case ValueType::Bool:
70 return phasor_make_bool(cpp_value.asBool());
71 case ValueType::Int:
72 return phasor_make_int(cpp_value.asInt());
74 return phasor_make_float(cpp_value.asFloat());
75 case ValueType::String: {
76 const auto &str = cpp_value.asString();
77 auto c_str = std::make_unique<char[]>(str.length() + 1);
78 std::copy(str.begin(), str.end(), c_str.get());
79 c_str[str.length()] = '\0';
80 PhasorValue val = phasor_make_string(c_str.get());
81 string_arena.push_back(std::move(c_str));
82 return val;
83 }
84 case ValueType::Array: {
85 const auto &cpp_array = *cpp_value.asArray();
86 size_t count = cpp_array.size();
87 if (count == 0)
88 {
89 return phasor_make_array(nullptr, 0);
90 }
91
92 auto c_array = std::make_unique<PhasorValue[]>(count);
93 for (size_t i = 0; i < count; ++i)
94 {
95 c_array[i] = to_c_value(cpp_array[i], string_arena, array_arena);
96 }
97
98 PhasorValue val = phasor_make_array(c_array.get(), count);
99 array_arena.push_back(std::move(c_array));
100 return val;
101 }
102 default:
103 return phasor_make_null();
104 }
105}
106
107Phasor::Value c_native_func_wrapper(PhasorNativeFunction c_func, Phasor::VM *vm, const std::vector<Phasor::Value> &args)
108{
109 std::vector<std::unique_ptr<char[]>> string_arena;
110 std::vector<std::unique_ptr<PhasorValue[]>> array_arena;
111
112 std::vector<PhasorValue> c_args;
113 c_args.reserve(args.size());
114 for (const auto &arg : args)
115 {
116 c_args.push_back(to_c_value(arg, string_arena, array_arena));
117 }
118
119 PhasorValue c_result = c_func(reinterpret_cast<PhasorVM *>(vm), (int)c_args.size(), c_args.data());
120
121 return from_c_value(c_result);
122}
123
124void register_native_c_func(PhasorVM *vm, const char *name, PhasorNativeFunction func)
125{
126 Phasor::VM *cpp_vm = reinterpret_cast<Phasor::VM *>(vm);
127
128 auto wrapper = [func](const std::vector<Phasor::Value> &args, Phasor::VM *vm_param) -> Phasor::Value {
129 return c_native_func_wrapper(func, vm_param, args);
130 };
131
132 cpp_vm->registerNativeFunction(name, wrapper);
133}
134
135} // 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:204
static PhasorValue phasor_make_array(const PhasorValue *elements, size_t count)
Definition PhasorFFI.h:139
static PhasorValue phasor_make_float(double f)
Definition PhasorFFI.h:123
static PhasorValue phasor_make_string(const char *s)
Definition PhasorFFI.h:131
static PhasorValue phasor_make_null()
Definition PhasorFFI.h:100
static PhasorValue phasor_make_bool(bool b)
Definition PhasorFFI.h:107
static PhasorValue phasor_make_int(int64_t i)
Definition PhasorFFI.h:115
struct PhasorVM PhasorVM
Phasor Virtual Machine pointer.
Definition PhasorFFI.h:59
@ PHASOR_TYPE_FLOAT
Definition PhasorFFI.h:67
@ PHASOR_TYPE_NULL
Definition PhasorFFI.h:64
@ PHASOR_TYPE_ARRAY
Definition PhasorFFI.h:69
@ PHASOR_TYPE_STRING
Definition PhasorFFI.h:68
@ PHASOR_TYPE_BOOL
Definition PhasorFFI.h:65
@ PHASOR_TYPE_INT
Definition PhasorFFI.h:66
Virtual Machine.
Definition VM.hpp:33
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:58
std::shared_ptr< ArrayInstance > asArray()
Get the value as an array.
Definition Value.hpp:194
ValueType getType() const noexcept
Get the type of the value.
Definition Value.hpp:113
int64_t asInt() const noexcept
Get the value as an integer.
Definition Value.hpp:159
double asFloat() const noexcept
Get the value as a double.
Definition Value.hpp:172
bool asBool() const noexcept
Get the value as a boolean.
Definition Value.hpp:154
std::string asString() const noexcept
Get the value as a string.
Definition Value.hpp:185
static Value createArray(std::vector< Value > elements={})
Definition Value.hpp:532
The Phasor Programming Language and Runtime.
Definition AST.hpp:12
void register_native_c_func(PhasorVM *vm, const char *name, PhasorNativeFunction func)
The concrete implementation of the PhasorRegisterFunction API call.
Definition api.cpp:124
Phasor::Value from_c_value(const PhasorValue &c_value)
Converts a C-style FFI value to a C++ VM value.
Definition api.cpp:18
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:107
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:62
Represents a value in the Phasor VM.
Definition PhasorFFI.h:79
struct PhasorValue::@257106260015165034006071221344325333366100147341::@324103060207145140324203224012174236216253240107 a
const PhasorValue * elements
Definition PhasorFFI.h:89
const char * s
Definition PhasorFFI.h:85
int64_t i
Definition PhasorFFI.h:83
PhasorValueType type
Definition PhasorFFI.h:80
union PhasorValue::@257106260015165034006071221344325333366100147341 as
size_t count
Definition PhasorFFI.h:90
double f
Definition PhasorFFI.h:84