Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
LanderlYoung committed Feb 8, 2024
1 parent 076cfdd commit d4faea7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
11 changes: 4 additions & 7 deletions backend/Python/PyHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ PyObject* checkException(PyObject* obj) {
return obj;
}

int checkException(int ret) {
if (ret == -1) {
checkException();
}
return ret;
}

void checkException() {
auto err = PyErr_Occurred();
if (err) {
// TODO
}
}

void rethrowException(const Exception& exception) {
// TODO
}

} // namespace script::py_backend
2 changes: 1 addition & 1 deletion backend/Python/PyHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace script::py_backend {
class PyEngine;

PyObject* checkException(PyObject* obj);
int checkException(int ret);
void checkException();
void rethrowException(const Exception& exception);

} // namespace script::py_backend
1 change: 1 addition & 0 deletions backend/Python/PyHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once
#include "../../src/Native.hpp"
#include "../../src/Reference.h"
#include "PyEngine.h"
#include "PyHelper.h"

namespace script {
Expand Down
26 changes: 23 additions & 3 deletions backend/Python/PyLocalReference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "../../src/Reference.h"
#include "../../src/Utils.h"
#include "../../src/Value.h"
#include "PyHelper.hpp"
#include "PyReference.hpp"

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

#define REF_IMPL_TO_VALUE(ValueType) \
Local<Value> Local<ValueType>::asValue() const { return Local<Value>(val_); }
Local<Value> Local<ValueType>::asValue() const { return Local<Value>(py_backend::incRef(val_)); }

REF_IMPL_BASIC_FUNC(Value)

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

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

bool Local<Value>::isFunction() const { return false; }
bool Local<Value>::isFunction() const { return PyCallable_Check(val_); }

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

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

Local<Value> Local<Function>::callImpl(const Local<Value>& thiz, size_t size,
const Local<Value>* args) const {
return {};
// PyObject* self = thiz.isObject() ? py_interop::toPy(thiz) : nullptr;
// TODO: self
PyObject* ret = nullptr;
// args to tuple
if (size == 0) {
ret = PyObject_CallNoArgs(py_interop::asPy(*this));
} else if (size == 1) {
ret = PyObject_CallOneArg(py_interop::asPy(*this), py_interop::asPy(args[0]));
} else {
auto tuple = PyTuple_New(static_cast<Py_ssize_t>(size));
py_backend::checkException();
for (size_t i = 0; i < size; ++i) {
PyTuple_SetItem(tuple, static_cast<Py_ssize_t>(i), py_interop::toPy(args[i]));
py_backend::checkException();
}
ret = PyObject_Call(py_interop::asPy(*this), tuple, nullptr);
}

py_backend::checkException();
return Local<Value>(ret);
}

size_t Local<Array>::size() const { return 0; }
Expand Down
14 changes: 7 additions & 7 deletions backend/Python/PyValue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include "../../src/Value.h"
#include "PyHelper.hpp"

using script::py_interop;
using script::py_backend::checkException;
using script::py_backend::py_interop;

namespace script {

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

namespace {

static constexpr const char* kFunctionDataName = "capsule_function_data";
static constexpr const char* kFunctionDataName = "_ScriptX_function_data";

struct FunctionData {
FunctionCallback function;
Expand All @@ -108,14 +108,14 @@ Local<Function> Function::newFunction(script::FunctionCallback callback) {
method.ml_meth = [](PyObject* self, PyObject* args) -> PyObject* {
auto ptr = PyCapsule_GetPointer(self, kFunctionDataName);
if (ptr == nullptr) {
// TODO: exception
::PyErr_SetString(PyExc_TypeError, "invalid 'self' for native method");
} else {
auto data = static_cast<FunctionData*>(ptr);
try {
auto ret = data->function(py_interop::makeArguments(nullptr, self, args));
return py_interop::toPy(ret);
} catch (Exception& e) {
// TODO: exception
py_backend::rethrowException(e);
}
}
return nullptr;
Expand All @@ -125,13 +125,13 @@ Local<Function> Function::newFunction(script::FunctionCallback callback) {
auto ptr = PyCapsule_GetPointer(cap, kFunctionDataName);
delete static_cast<FunctionData*>(ptr);
});
py_backend::checkException(ctx);
callbackIns.release();

PyObject* closure = PyCFunction_New(&method, ctx);

Py_XDECREF(ctx);
py_backend::checkException(closure);

// todo: check exception
callbackIns.release();
return Local<Function>(closure);
}

Expand Down

0 comments on commit d4faea7

Please sign in to comment.