|
1 | 1 | #pragma once |
2 | 2 | #include "DeclarationGenerator.h" |
3 | 3 | #include "JSClassRegister.h" |
| 4 | +#include "ScriptBackend.hpp" |
| 5 | +#include "V8Backend.hpp" |
| 6 | +#include "endstone/command/command_executor.h" |
| 7 | +#include "endstone/plugin/plugin.h" |
| 8 | +#include "manager/NodeManager.h" |
4 | 9 | #include "manager/V8Engine.h" |
5 | 10 | #include "v8-context.h" |
| 11 | +#include "v8-exception.h" |
6 | 12 | #include "v8-external.h" |
7 | 13 | #include "v8-function-callback.h" |
| 14 | +#include "v8-isolate.h" |
8 | 15 | #include "v8-local-handle.h" |
| 16 | +#include "v8-locker.h" |
9 | 17 | #include "v8-primitive.h" |
10 | 18 | #include "v8-template.h" |
11 | 19 | #include "v8-value.h" |
| 20 | +#include "v8_utils/V8Exception.h" |
| 21 | +#include "v8_utils/V8Util.h" |
| 22 | +#include <iostream> |
12 | 23 | #include <string> |
13 | 24 |
|
| 25 | + |
| 26 | +UsingCppType(endstone::Plugin); |
| 27 | +UsingCppType(endstone::CommandExecutor); |
| 28 | + |
| 29 | +struct AutoBinding { |
| 30 | + AutoBinding() { |
| 31 | + puerts::DefineClass<endstone::CommandExecutor>().Register(); |
| 32 | + |
| 33 | + puerts::DefineClass<endstone::Plugin>() |
| 34 | + .Extends<endstone::CommandExecutor>() |
| 35 | + // .Method("getDescription", &endstone::Plugin::getDescription) |
| 36 | + .Method("onLoad", MakeFunction(&endstone::Plugin::onLoad)) |
| 37 | + .Method("onEnable", MakeFunction(&endstone::Plugin::onEnable)) |
| 38 | + .Method("onDisable", MakeFunction(&endstone::Plugin::onDisable)) |
| 39 | + .Method("getName", MakeFunction(&endstone::Plugin::getName)) |
| 40 | + .Register(); |
| 41 | + } |
| 42 | +}; |
| 43 | +AutoBinding __AutoBinding__; |
| 44 | + |
| 45 | + |
14 | 46 | namespace jse { |
15 | 47 |
|
| 48 | + |
| 49 | +inline void Js_RegisterPlugin(const v8::FunctionCallbackInfo<v8::Value>& args) {} |
| 50 | + |
| 51 | +inline void Js_GetPlugin(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 52 | + auto isolate = v8::Isolate::GetCurrent(); |
| 53 | + |
| 54 | + v8::Isolate::Scope isolate_scope(isolate); |
| 55 | + v8::HandleScope handle_scope(isolate); |
| 56 | + |
| 57 | + auto ctx = isolate->GetCurrentContext(); |
| 58 | + |
| 59 | + v8::Context::Scope context_scope(ctx); |
| 60 | + v8::TryCatch vtry{isolate}; |
| 61 | + |
| 62 | + auto _IdVal = ctx->Global()->Get(ctx, v8::String::NewFromUtf8Literal(isolate, "__ENGINE_ID__")).ToLocalChecked(); |
| 63 | + auto _DoubleId = _IdVal->ToNumber(ctx).ToLocalChecked()->Value(); |
| 64 | + auto id = static_cast<EngineID>(_DoubleId); |
| 65 | + |
| 66 | + std::cout << "GetEngine id: " << id << std::endl; |
| 67 | + |
| 68 | + auto engine = NodeManager::getInstance().getEngine(id); |
| 69 | + v8_exception::checkTryCatch(vtry); |
| 70 | +} |
| 71 | + |
| 72 | +inline void RegisterEngineApi() { |
| 73 | + auto isolate = v8::Isolate::GetCurrent(); |
| 74 | + |
| 75 | + v8::Locker locker(isolate); |
| 76 | + v8::Isolate::Scope isolate_scope(isolate); |
| 77 | + v8::HandleScope handle_scope(isolate); |
| 78 | + |
| 79 | + auto ctx = v8::Context::New(isolate); |
| 80 | + v8::Context::Scope context_scope(ctx); |
| 81 | + |
| 82 | + // Engine.registerPlugin() |
| 83 | + // Engine.getSelf() |
| 84 | + auto tpl = v8::ObjectTemplate::New(isolate); |
| 85 | + tpl->Set( |
| 86 | + v8::String::NewFromUtf8Literal(isolate, "registerPlugin"), |
| 87 | + v8::FunctionTemplate::New(isolate, Js_RegisterPlugin) |
| 88 | + ); |
| 89 | + |
| 90 | + tpl->Set(v8::String::NewFromUtf8Literal(isolate, "getSelf"), v8::FunctionTemplate::New(isolate, Js_GetPlugin)); |
| 91 | + |
| 92 | + tpl->NewInstance(ctx).ToLocalChecked(); |
| 93 | + ctx->Global() |
| 94 | + ->Set(ctx, v8::String::NewFromUtf8Literal(isolate, "Engine"), tpl->NewInstance(ctx).ToLocalChecked()) |
| 95 | + .Check(); |
| 96 | +} |
| 97 | + |
16 | 98 | inline void RegisterGlobalFunc(V8Engine* wrapper) { |
17 | 99 | auto isolate = wrapper->isolate(); |
18 | 100 | auto ctx = wrapper->context(); |
@@ -62,13 +144,13 @@ inline void RegisterGlobalFunc(V8Engine* wrapper) { |
62 | 144 | ) |
63 | 145 | .Check(); |
64 | 146 |
|
65 | | - global |
66 | | - ->Set( |
67 | | - ctx, |
68 | | - v8::String::NewFromUtf8(isolate, "__ENGINE_ID__").ToLocalChecked(), |
69 | | - v8::Number::New(isolate, static_cast<double>(wrapper->mID)) |
70 | | - ) |
71 | | - .Check(); |
| 147 | + v8_util::DefineReadOnlyGlobal( |
| 148 | + isolate, |
| 149 | + "__ENGINE_ID__", |
| 150 | + v8::Number::New(isolate, static_cast<double>(wrapper->mID)) |
| 151 | + ); |
| 152 | + |
| 153 | + RegisterEngineApi(); |
72 | 154 | } |
73 | 155 |
|
74 | 156 |
|
|
0 commit comments