Skip to content

Commit 7d6247c

Browse files
committed
wip
1 parent b05e92c commit 7d6247c

File tree

12 files changed

+111
-24
lines changed

12 files changed

+111
-24
lines changed

backend/Python/PyEngine.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717

1818
#include "PyEngine.h"
1919
#include "../../src/Utils.h"
20+
#include "pydebug.h"
21+
#include "pylifecycle.h"
2022

2123
namespace script::py_backend {
2224

23-
PyEngine::PyEngine(std::shared_ptr<utils::MessageQueue> queue) {}
25+
PyEngine::PyEngine(std::shared_ptr<utils::MessageQueue> queue) { Py_Initialize(); }
2426

2527
PyEngine::PyEngine() : PyEngine(std::shared_ptr<utils::MessageQueue>{}) {}
2628

2729
PyEngine::~PyEngine() = default;
2830

29-
void PyEngine::destroy() noexcept {}
31+
void PyEngine::destroy() noexcept { ScriptEngine::destroyUserData(); }
3032

3133
Local<Value> PyEngine::get(const Local<String>& key) { return Local<Value>(); }
3234

@@ -50,9 +52,9 @@ void PyEngine::gc() {}
5052

5153
void PyEngine::adjustAssociatedMemory(int64_t count) {}
5254

53-
ScriptLanguage PyEngine::getLanguageType() { return ScriptLanguage::kJavaScript; }
55+
ScriptLanguage PyEngine::getLanguageType() { return ScriptLanguage::kPython; }
5456

55-
std::string PyEngine::getEngineVersion() { return ""; }
57+
std::string PyEngine::getEngineVersion() { return Py_GetVersion(); }
5658

5759
bool PyEngine::isDestroying() const { return false; }
5860

backend/Python/PyEngine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "../../src/Engine.h"
2121
#include "../../src/Exception.h"
2222
#include "../../src/utils/MessageQueue.h"
23+
#include "PyHelper.h"
2324

2425
namespace script::py_backend {
2526

backend/Python/PyHelper.cc

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,29 @@
1515
* limitations under the License.
1616
*/
1717

18-
#include "PyHelper.hpp"
18+
#include "PyHelper.hpp"
19+
20+
namespace script::py_backend {
21+
22+
PyObject* checkException(PyObject* obj) {
23+
if (!obj) {
24+
checkException();
25+
}
26+
return obj;
27+
}
28+
29+
int checkException(int ret) {
30+
if (ret == -1) {
31+
checkException();
32+
}
33+
return ret;
34+
}
35+
36+
void checkException() {
37+
auto err = PyErr_Occurred();
38+
if (err) {
39+
// TODO
40+
}
41+
}
42+
43+
} // namespace script::py_backend

backend/Python/PyHelper.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
#include "../../src/foundation.h"
2121

22-
// docs: https://docs.python.org/3/c-api/index.html
22+
// docs:
23+
// https://docs.python.org/3/c-api/index.html
24+
// https://docs.python.org/3/extending/embedding.html
25+
// https://docs.python.org/2/c-api/init.html#thread-state-and-the-global-interpreter-lock
2326

2427
SCRIPTX_BEGIN_INCLUDE_LIBRARY
2528
#ifndef PY_SSIZE_T_CLEAN
@@ -28,4 +31,10 @@ SCRIPTX_BEGIN_INCLUDE_LIBRARY
2831
#include <Python.h>
2932
SCRIPTX_END_INCLUDE_LIBRARY
3033

31-
namespace script::py_backend {}
34+
namespace script::py_backend {
35+
36+
PyObject* checkException(PyObject* obj);
37+
int checkException(int ret);
38+
void checkException();
39+
40+
} // namespace script::py_backend

backend/Python/PyHelper.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
*/
1717

1818
#pragma once
19+
#include "../../src/Reference.h"
1920
#include "PyHelper.h"
2021

21-
namespace script::py_backend {}
22+
namespace script::py_backend {
23+
24+
struct py_interop {
25+
template <class T>
26+
static Local<T> makeLocal(PyObject* ref) {
27+
return Local<T>(ref);
28+
}
29+
};
30+
31+
} // namespace script::py_backend

backend/Python/PyValue.cc

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,39 @@
1919
#include "../../src/Reference.h"
2020
#include "../../src/Scope.h"
2121
#include "../../src/Value.h"
22+
#include "PyHelper.hpp"
23+
24+
using script::py_backend::checkException;
25+
using script::py_backend::py_interop;
2226

2327
namespace script {
2428

25-
Local<Object> Object::newObject() { TEMPLATE_NOT_IMPLEMENTED(); }
29+
template <typename T>
30+
Local<T> checkAndMakeLocal(PyObject* ref) {
31+
return py_interop::makeLocal<T>(checkException(ref));
32+
}
33+
34+
// for python this creates an empty dict
35+
Local<Object> Object::newObject() { return checkAndMakeLocal<Object>(PyDict_New()); }
2636

2737
Local<Object> Object::newObjectImpl(const Local<Value>& type, size_t size,
2838
const Local<Value>* args) {
2939
TEMPLATE_NOT_IMPLEMENTED();
3040
}
3141

32-
Local<String> String::newString(const char* utf8) { TEMPLATE_NOT_IMPLEMENTED(); }
42+
Local<String> String::newString(const char* utf8) {
43+
return checkAndMakeLocal<String>(PyBytes_FromString(utf8));
44+
}
3345

34-
Local<String> String::newString(std::string_view utf8) { TEMPLATE_NOT_IMPLEMENTED(); }
46+
Local<String> String::newString(std::string_view utf8) {
47+
return checkAndMakeLocal<String>(
48+
PyBytes_FromStringAndSize(utf8.data(), static_cast<Py_ssize_t>(utf8.length())));
49+
}
3550

36-
Local<String> String::newString(const std::string& utf8) { TEMPLATE_NOT_IMPLEMENTED(); }
51+
Local<String> String::newString(const std::string& utf8) {
52+
return checkAndMakeLocal<String>(
53+
PyBytes_FromStringAndSize(utf8.c_str(), static_cast<Py_ssize_t>(utf8.length())));
54+
}
3755

3856
#if defined(__cpp_char8_t)
3957

@@ -51,19 +69,29 @@ Local<String> String::newString(const std::u8string& utf8) { return newString(ut
5169

5270
Local<Number> Number::newNumber(float value) { return newNumber(static_cast<double>(value)); }
5371

54-
Local<Number> Number::newNumber(double value) { TEMPLATE_NOT_IMPLEMENTED(); }
72+
Local<Number> Number::newNumber(double value) {
73+
return checkAndMakeLocal<Number>(PyLong_FromDouble(value));
74+
}
5575

56-
Local<Number> Number::newNumber(int32_t value) { return newNumber(static_cast<double>(value)); }
76+
Local<Number> Number::newNumber(int32_t value) {
77+
return checkAndMakeLocal<Number>(PyLong_FromLong(static_cast<long>(value)));
78+
}
5779

58-
Local<Number> Number::newNumber(int64_t value) { return newNumber(static_cast<double>(value)); }
80+
Local<Number> Number::newNumber(int64_t value) {
81+
return checkAndMakeLocal<Number>(PyLong_FromLongLong(static_cast<long long>(value)));
82+
}
5983

60-
Local<Boolean> Boolean::newBoolean(bool value) { TEMPLATE_NOT_IMPLEMENTED(); }
84+
Local<Boolean> Boolean::newBoolean(bool value) {
85+
return checkAndMakeLocal<Boolean>(PyBool_FromLong(value));
86+
}
6187

6288
Local<Function> Function::newFunction(script::FunctionCallback callback) {
6389
TEMPLATE_NOT_IMPLEMENTED();
6490
}
6591

66-
Local<Array> Array::newArray(size_t size) { TEMPLATE_NOT_IMPLEMENTED(); }
92+
Local<Array> Array::newArray(size_t size) {
93+
return checkAndMakeLocal<Array>(PyList_New(static_cast<Py_ssize_t>(size)));
94+
}
6795

6896
Local<Array> Array::newArrayImpl(size_t size, const Local<Value>* args) {
6997
TEMPLATE_NOT_IMPLEMENTED();

backend/Python/trait/TraitException.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717

1818
#pragma once
19+
#include <string>
20+
#include "../../src/types.h"
1921

2022
namespace script {
2123

backend/Python/trait/TraitIncludes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
#include "../PyReference.hpp"
2323

2424
// global marco
25-
#define SCRIPTX_BACKEND_TEMPLATE
26-
#define SCRIPTX_LANG_NONE
25+
#define SCRIPTX_BACKEND_PYTHON
26+
#define SCRIPTX_LANG_PYTHON

backend/Python/trait/TraitNative.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#pragma once
19+
#include "../../src/types.h"
1920

2021
namespace script {
2122

backend/Python/trait/TraitReference.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@
2020
#include <cstdint>
2121
#include <string>
2222
#include "../../src/types.h"
23+
#include "../PyHelper.h"
2324

2425
namespace script::internal {
2526

2627
template <typename T>
2728
struct ImplType<Local<T>> {
28-
using type = int;
29+
using type = PyObject*;
2930
};
3031

3132
template <typename T>
3233
struct ImplType<Global<T>> {
33-
using type = int;
34+
using type = PyObject*;
3435
};
3536

3637
template <typename T>
3738
struct ImplType<Weak<T>> {
38-
using type = int;
39+
using type = PyObject*;
3940
};
4041

4142
} // namespace script::internal

0 commit comments

Comments
 (0)