Files

176 lines
6.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// P4-43/P4-60 官方语言绑定元信息插件(ABI 1.9;纯 C ABI,不链接内核)。
// ABI 1.9:get_manifest_json_ex(host, options_json, result) 支持
// options {"language":"python|csharp|typescript"};旧 get_manifest_json 仍支持,
// 语言从 VDE_BINDING_LANGUAGE 环境变量选择(python 默认)以兼容旧宿主/调用方。
// 未知语言返回 VDE_PLUGIN_ERR_INVALID_ARG + set_error。输出确定性 JSON:
// binding_id/language/version/module/entry/symbols/type_mappings/build_hints。
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
#include "vde/plugin/vde_plugin_api.h"
namespace {
constexpr const char* kBindingId = "binding.python";
constexpr const char* kApiVersion = "1.0";
std::string selected_language() {
const char* raw = std::getenv("VDE_BINDING_LANGUAGE");
if (raw == nullptr || *raw == '\0') return "python";
std::string language(raw);
for (char& c : language) {
if (c >= 'A' && c <= 'Z') c = static_cast<char>(c - 'A' + 'a');
}
return language;
}
std::string language_from_options(const char* options_json) {
if (!options_json) return selected_language();
const char* key = std::strstr(options_json, "\"language\"");
if (!key) return selected_language();
const char* colon = std::strchr(key, ':');
if (!colon) return selected_language();
const char* begin = colon + 1;
while (*begin == ' ' || *begin == '\t') ++begin;
if (*begin != '"') return selected_language();
++begin;
const char* end = std::strchr(begin, '"');
if (!end) return selected_language();
std::string language(begin, static_cast<std::size_t>(end - begin));
for (char& c : language) {
if (c >= 'A' && c <= 'Z') c = static_cast<char>(c - 'A' + 'a');
}
return language;
}
const char* manifest_for_language(const std::string& language) {
if (language == "python") {
return "{\"binding_id\":\"binding.python\",\"language\":\"python\","
"\"version\":\"1.0\",\"module\":\"vde\",\"entry\":\"vde._ctypes\","
"\"symbols\":[\"vde_body_create_box\",\"vde_body_volume\","
"\"vde_body_destroy\"],\"type_mappings\":{"
"\"Vector3D\":\"tuple[float, float, float]\","
"\"BrepModel\":\"vde._ctypes.Body\"},\"build_hints\":{"
"\"native\":\"vde_capi shared library\","
"\"smoke\":\"python python/smoke_test_ctypes.py --library "
"<build>/src/libvde_capi.so\"}}";
}
if (language == "csharp") {
return "{\"binding_id\":\"binding.python\",\"language\":\"csharp\","
"\"version\":\"1.0\",\"module\":\"Vde.Native\","
"\"entry\":\"Vde.Native.NativeMethods\","
"\"symbols\":[\"CreateBox\",\"GetVolume\",\"DestroyBody\"],"
"\"type_mappings\":{\"Vector3D\":\"System.Numerics.Vector3\","
"\"BrepModel\":\"Vde.Native.BodyHandle\"},\"build_hints\":{"
"\"native\":\"vde_capi shared library\","
"\"interop\":\"P/Invoke (DllImport)\"}}";
}
if (language == "typescript") {
return "{\"binding_id\":\"binding.python\",\"language\":\"typescript\","
"\"version\":\"1.0\",\"module\":\"@vde/core\",\"entry\":\"createBox\","
"\"symbols\":[\"createBox\",\"bodyVolume\",\"destroyBody\"],"
"\"type_mappings\":{\"Vector3D\":\"[number, number, number]\","
"\"BrepModel\":\"BodyHandle\"},\"build_hints\":{"
"\"wasm\":\"vde_wasm (planned)\",\"types\":\"index.d.ts\"}}";
}
return nullptr;
}
int write_manifest(const VdePluginHost* host, const std::string& language,
VdeBuffer* result_json) {
if (result_json) {
result_json->data = nullptr;
result_json->size = 0;
}
if (!host || !host->alloc || !host->set_error || !result_json)
return VDE_PLUGIN_ERR_INVALID_ARG;
const char* json = manifest_for_language(language);
if (json == nullptr) {
host->set_error(("binding_meta: unknown language '" + language +
"' (expect python|csharp|typescript)").c_str());
return VDE_PLUGIN_ERR_INVALID_ARG;
}
const std::size_t size = std::strlen(json);
void* out = host->alloc(size + 1);
if (!out) {
host->set_error("binding_meta: alloc failed");
return VDE_PLUGIN_ERR_BUFFER;
}
std::memcpy(out, json, size + 1);
result_json->data = out;
result_json->size = size;
return VDE_PLUGIN_OK;
}
int get_manifest_json(const VdePluginHost* host, VdeBuffer* result_json) {
return write_manifest(host, selected_language(), result_json);
}
int get_manifest_json_ex(const VdePluginHost* host, const char* options_json,
VdeBuffer* result_json) {
return write_manifest(host, language_from_options(options_json), result_json);
}
const char* describe() {
return "VDE official binding metadata (python/csharp/typescript; "
"options/VDE_BINDING_LANGUAGE; ABI 1.9)";
}
const VdeBindingPlugin kBinding = [] {
VdeBindingPlugin plugin{};
plugin.struct_size = sizeof(VdeBindingPlugin);
plugin.binding_id = kBindingId;
plugin.language = "python"; // 注册默认语言;manifest 由 VDE_BINDING_LANGUAGE 选择
plugin.capabilities_json =
"{\"languages\":[\"python\",\"csharp\",\"typescript\"],"
"\"module\":\"vde\",\"api_version\":\"1.0\","
"\"selection\":\"options.language|VDE_BINDING_LANGUAGE\"}";
plugin.get_manifest_json = &get_manifest_json;
plugin.get_manifest_json_ex = &get_manifest_json_ex;
plugin.describe = &describe;
return plugin;
}();
} // namespace
extern "C" {
VDE_PLUGIN_EXPORT int vde_plugin_query(std::uint32_t host_abi_major, VdePluginInfo* info) {
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_INVALID_ARG;
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
info->id = "com.vde.official.binding.python";
info->name = "VDE Official Binding Metadata";
info->version = "1.0.0";
info->kind = VDE_PLUGIN_KIND_BINDING;
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
info->capabilities = "binding:python;binding:csharp;binding:typescript";
info->license = "Apache-2.0";
return VDE_PLUGIN_OK;
}
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) {
(void)host;
if (!reg) return VDE_PLUGIN_ERR_INVALID_ARG;
if (reg->struct_size < sizeof(VdePluginRegistration)) return VDE_PLUGIN_ERR_ABI;
reg->kind = VDE_PLUGIN_KIND_BINDING;
reg->format = nullptr;
reg->generic = nullptr;
reg->cae_solver = nullptr;
reg->cam_strategy = nullptr;
reg->gcs_solver = nullptr;
reg->mesher = nullptr;
reg->optimizer = nullptr;
reg->io_batch = nullptr;
reg->geom_op = nullptr;
reg->binding = &kBinding;
return VDE_PLUGIN_OK;
}
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
} // extern "C"