Skip to content

Commit d4faea7

Browse files
committed
wip
1 parent 076cfdd commit d4faea7

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

backend/Python/PyHelper.cc

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,15 @@ PyObject* checkException(PyObject* obj) {
2626
return obj;
2727
}
2828

29-
int checkException(int ret) {
30-
if (ret == -1) {
31-
checkException();
32-
}
33-
return ret;
34-
}
35-
3629
void checkException() {
3730
auto err = PyErr_Occurred();
3831
if (err) {
3932
// TODO
4033
}
4134
}
4235

36+
void rethrowException(const Exception& exception) {
37+
// TODO
38+
}
39+
4340
} // namespace script::py_backend

backend/Python/PyHelper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace script::py_backend {
3737
class PyEngine;
3838

3939
PyObject* checkException(PyObject* obj);
40-
int checkException(int ret);
4140
void checkException();
41+
void rethrowException(const Exception& exception);
4242

4343
} // namespace script::py_backend

backend/Python/PyHelper.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#pragma once
1919
#include "../../src/Native.hpp"
2020
#include "../../src/Reference.h"
21+
#include "PyEngine.h"
2122
#include "PyHelper.h"
2223

2324
namespace script {

backend/Python/PyLocalReference.cc

+23-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "../../src/Reference.h"
2020
#include "../../src/Utils.h"
2121
#include "../../src/Value.h"
22+
#include "PyHelper.hpp"
2223
#include "PyReference.hpp"
2324

2425
namespace script {
@@ -61,7 +62,7 @@ void valueConstructorCheck(PyObject* value) {
6162
std::string Local<ValueType>::describeUtf8() const { return asValue().describeUtf8(); }
6263

6364
#define REF_IMPL_TO_VALUE(ValueType) \
64-
Local<Value> Local<ValueType>::asValue() const { return Local<Value>(val_); }
65+
Local<Value> Local<ValueType>::asValue() const { return Local<Value>(py_backend::incRef(val_)); }
6566

6667
REF_IMPL_BASIC_FUNC(Value)
6768

@@ -146,7 +147,7 @@ bool Local<Value>::isNumber() const { return PyNumber_Check(val_); }
146147

147148
bool Local<Value>::isBoolean() const { return PyBool_Check(val_); }
148149

149-
bool Local<Value>::isFunction() const { return false; }
150+
bool Local<Value>::isFunction() const { return PyCallable_Check(val_); }
150151

151152
bool Local<Value>::isArray() const { return false; }
152153

@@ -217,7 +218,26 @@ bool Local<Boolean>::value() const { return false; }
217218

218219
Local<Value> Local<Function>::callImpl(const Local<Value>& thiz, size_t size,
219220
const Local<Value>* args) const {
220-
return {};
221+
// PyObject* self = thiz.isObject() ? py_interop::toPy(thiz) : nullptr;
222+
// TODO: self
223+
PyObject* ret = nullptr;
224+
// args to tuple
225+
if (size == 0) {
226+
ret = PyObject_CallNoArgs(py_interop::asPy(*this));
227+
} else if (size == 1) {
228+
ret = PyObject_CallOneArg(py_interop::asPy(*this), py_interop::asPy(args[0]));
229+
} else {
230+
auto tuple = PyTuple_New(static_cast<Py_ssize_t>(size));
231+
py_backend::checkException();
232+
for (size_t i = 0; i < size; ++i) {
233+
PyTuple_SetItem(tuple, static_cast<Py_ssize_t>(i), py_interop::toPy(args[i]));
234+
py_backend::checkException();
235+
}
236+
ret = PyObject_Call(py_interop::asPy(*this), tuple, nullptr);
237+
}
238+
239+
py_backend::checkException();
240+
return Local<Value>(ret);
221241
}
222242

223243
size_t Local<Array>::size() const { return 0; }

backend/Python/PyValue.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include "../../src/Value.h"
2222
#include "PyHelper.hpp"
2323

24+
using script::py_interop;
2425
using script::py_backend::checkException;
25-
using script::py_backend::py_interop;
2626

2727
namespace script {
2828

@@ -87,7 +87,7 @@ Local<Boolean> Boolean::newBoolean(bool value) {
8787

8888
namespace {
8989

90-
static constexpr const char* kFunctionDataName = "capsule_function_data";
90+
static constexpr const char* kFunctionDataName = "_ScriptX_function_data";
9191

9292
struct FunctionData {
9393
FunctionCallback function;
@@ -108,14 +108,14 @@ Local<Function> Function::newFunction(script::FunctionCallback callback) {
108108
method.ml_meth = [](PyObject* self, PyObject* args) -> PyObject* {
109109
auto ptr = PyCapsule_GetPointer(self, kFunctionDataName);
110110
if (ptr == nullptr) {
111-
// TODO: exception
111+
::PyErr_SetString(PyExc_TypeError, "invalid 'self' for native method");
112112
} else {
113113
auto data = static_cast<FunctionData*>(ptr);
114114
try {
115115
auto ret = data->function(py_interop::makeArguments(nullptr, self, args));
116116
return py_interop::toPy(ret);
117117
} catch (Exception& e) {
118-
// TODO: exception
118+
py_backend::rethrowException(e);
119119
}
120120
}
121121
return nullptr;
@@ -125,13 +125,13 @@ Local<Function> Function::newFunction(script::FunctionCallback callback) {
125125
auto ptr = PyCapsule_GetPointer(cap, kFunctionDataName);
126126
delete static_cast<FunctionData*>(ptr);
127127
});
128+
py_backend::checkException(ctx);
129+
callbackIns.release();
128130

129131
PyObject* closure = PyCFunction_New(&method, ctx);
130-
131132
Py_XDECREF(ctx);
133+
py_backend::checkException(closure);
132134

133-
// todo: check exception
134-
callbackIns.release();
135135
return Local<Function>(closure);
136136
}
137137

0 commit comments

Comments
 (0)