From 6f4b0ce0946ae9eb3fa984fd603c81ce96c267b8 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 28 Apr 2022 22:55:22 +0200 Subject: [PATCH] Pass string_view instead of const char* to callback functions --- examples/calendar/main.cpp | 25 ++++++++++++++----------- examples/dieroll/main.cpp | 9 +++------ examples/greet/main.cpp | 13 +++++-------- include/wrenpp/def_configuration.hpp | 5 +++-- include/wrenpp/vm.hpp | 27 ++++++++++++++------------- include/wrenpp/wren_types.hpp | 22 ++++++++++++++++++++++ src/def_configuration.cpp | 4 ++-- 7 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 include/wrenpp/wren_types.hpp diff --git a/examples/calendar/main.cpp b/examples/calendar/main.cpp index f20fa87..9f797ec 100644 --- a/examples/calendar/main.cpp +++ b/examples/calendar/main.cpp @@ -97,8 +97,8 @@ System.print("You have %(cale.appointment_count()) appointment(s)") class MyConf : public wren::DefConfiguration { public: - char* load_module_fn(wren::VM* vm, const char* module_name) { - if (std::string_view{module_name} == "calendar") { + char* load_module_fn(wren::VM* vm, std::string_view module_name) { + if (module_name == "calendar") { constexpr const std::size_t buff_sz = sizeof(g_calendar_src); char* const buff = static_cast(MyConf::reallocate_fn(nullptr, buff_sz)); std::copy(g_calendar_src, g_calendar_src + buff_sz / sizeof(g_calendar_src[0]), buff); @@ -107,11 +107,13 @@ System.print("You have %(cale.appointment_count()) appointment(s)") return nullptr; } - wren::foreign_method_t foreign_method_fn(wren::VM* vm, const char* m, const char* c, bool is_static, const char* s) { - std::string_view module(m); - std::string_view class_name(c); - std::string_view signature(s); - + wren::foreign_method_t foreign_method_fn( + wren::VM* vm, + std::string_view module, + std::string_view class_name, + bool is_static, + std::string_view signature + ) { if (module == "calendar" and class_name == "Calendar") { if (is_static and signature == "today()") return &Calendar::today; @@ -123,10 +125,11 @@ System.print("You have %(cale.appointment_count()) appointment(s)") return nullptr; } - wren::foreign_class_t foreign_class_fn(wren::VM* vm, const char* m, const char* c) { - std::string_view module(m); - std::string_view class_name(c); - + wren::foreign_class_t foreign_class_fn( + wren::VM* vm, + std::string_view module, + std::string_view class_name + ) { if (module == "calendar" and class_name == "Calendar") { return wren::make_foreign_class(); } diff --git a/examples/dieroll/main.cpp b/examples/dieroll/main.cpp index d0396be..d84314e 100644 --- a/examples/dieroll/main.cpp +++ b/examples/dieroll/main.cpp @@ -51,14 +51,11 @@ namespace { public: wren::foreign_method_t foreign_method_fn ( wren::VM* vm, - const char* module_ptr, - const char* class_name_ptr, + std::string_view module, + std::string_view class_name, bool is_static, - const char* signature_ptr + std::string_view signature ) { - std::string_view module(module_ptr); - std::string_view class_name(class_name_ptr); - std::string_view signature(signature_ptr); //std::cout << "requested: \"" << module << "\" \"" << // class_name << "\" \"" << signature << "\" static: " << // std::boolalpha << is_static << '\n'; diff --git a/examples/greet/main.cpp b/examples/greet/main.cpp index 46debf4..f4ad9d6 100644 --- a/examples/greet/main.cpp +++ b/examples/greet/main.cpp @@ -63,18 +63,15 @@ class MyConfig : public wren::DefConfiguration { public: wren::foreign_method_t foreign_method_fn ( wren::VM* vm, - const char* module_ptr, - const char* class_name_ptr, + std::string_view module, + std::string_view class_name, bool is_static, - const char* signature_ptr + std::string_view signature ) { //std::cout << "VM " << vm << " requested foreign method " << - // class_name_ptr << "::" << signature_ptr << - // " in module " << module_ptr << "\n"; + // class_name << "::" << signature << + // " in module " << module << "\n"; - std::string_view module(module_ptr); - std::string_view class_name(class_name_ptr); - std::string_view signature(signature_ptr); if ("User" == class_name and "main" == module and "get_env(_)" == signature) return &user_get_env; diff --git a/include/wrenpp/def_configuration.hpp b/include/wrenpp/def_configuration.hpp index 0a26623..634e912 100644 --- a/include/wrenpp/def_configuration.hpp +++ b/include/wrenpp/def_configuration.hpp @@ -18,6 +18,7 @@ #pragma once #include "configuration.hpp" +#include "wrenpp/wren_types.hpp" #include "error_type.hpp" namespace wren { @@ -27,8 +28,8 @@ namespace wren { class DefConfiguration : public Configuration { public: - static void write_fn (VM*, const char* text); - static void error_fn (VM*, ErrorType, const char* module, int line, const char* msg); + static void write_fn (VM*, wren_string_t text); + static void error_fn (VM*, ErrorType, wren_string_t module, int line, wren_string_t msg); static void* reallocate_fn(void* ptr, std::size_t size); }; } //namespace wren diff --git a/include/wrenpp/vm.hpp b/include/wrenpp/vm.hpp index 926df0c..0c24a6b 100644 --- a/include/wrenpp/vm.hpp +++ b/include/wrenpp/vm.hpp @@ -21,6 +21,7 @@ #include "error_type.hpp" #include "handle.hpp" #include "StringCRC32.hpp" +#include "wren_types.hpp" #include #include #include @@ -49,14 +50,14 @@ namespace wren { namespace detail { struct Callbacks { - void (*write_fn)(Configuration&, VM*, const char*) {nullptr}; - void (*error_fn)(Configuration&, VM*, ErrorType, const char*, int, const char*) {nullptr}; + void (*write_fn)(Configuration&, VM*, wren_string_t) {nullptr}; + void (*error_fn)(Configuration&, VM*, ErrorType, wren_string_t, int, wren_string_t) {nullptr}; void* (*reallocate_fn)(void*, std::size_t) {nullptr}; - const char* (*resolve_module_fn)(Configuration&, VM*, const char*, const char*) {nullptr}; - char* (*load_module_fn)(Configuration&, VM*, const char*) {nullptr}; - foreign_method_t (*foreign_method_fn)(Configuration&, VM*, const char*, const char*, bool, const char*) {nullptr}; - foreign_class_t (*foreign_class_fn)(Configuration&, VM*, const char*, const char*) {nullptr}; - void (*load_module_complete_fn)(Configuration&, VM*, const char*); + const char* (*resolve_module_fn)(Configuration&, VM*, wren_string_t, wren_string_t) {nullptr}; + char* (*load_module_fn)(Configuration&, VM*, wren_string_t) {nullptr}; + foreign_method_t (*foreign_method_fn)(Configuration&, VM*, wren_string_t, wren_string_t, bool, wren_string_t) {nullptr}; + foreign_class_t (*foreign_class_fn)(Configuration&, VM*, wren_string_t, wren_string_t) {nullptr}; + void (*load_module_complete_fn)(Configuration&, VM*, wren_string_t); Configuration* config_obj {nullptr}; VM* owner {nullptr}; @@ -125,13 +126,13 @@ namespace wren { }; namespace detail { - define_method_info(write_fn, write, void, VM*, const char*); - define_method_info(error_fn, error, void, VM*, ErrorType, const char*, int, const char*); + define_method_info(write_fn, write, void, VM*, wren_string_t); + define_method_info(error_fn, error, void, VM*, ErrorType, wren_string_t, int, wren_string_t); define_method_info(reallocate_fn, reallocate, void*, void*, std::size_t); - define_method_info(resolve_module_fn, resolve_module, const char*, const char*, const char*); - define_method_info(load_module_fn, load_module, char*, VM*, const char*); - define_method_info(foreign_method_fn, foreign_method, foreign_method_t, VM*, const char*, const char*, bool, const char*); - define_method_info(foreign_class_fn, foreign_class, foreign_class_t, VM*, const char*, const char*); + define_method_info(resolve_module_fn, resolve_module, wren_string_t, wren_string_t, wren_string_t); + define_method_info(load_module_fn, load_module, char*, VM*, wren_string_t); + define_method_info(foreign_method_fn, foreign_method, foreign_method_t, VM*, wren_string_t, wren_string_t, bool, wren_string_t); + define_method_info(foreign_class_fn, foreign_class, foreign_class_t, VM*, wren_string_t, wren_string_t); template struct AnyFunctionWrap; template diff --git a/include/wrenpp/wren_types.hpp b/include/wrenpp/wren_types.hpp new file mode 100644 index 0000000..db9a6f4 --- /dev/null +++ b/include/wrenpp/wren_types.hpp @@ -0,0 +1,22 @@ +/* Copyright 2020-2022, Michele Santullo + * This file is part of wrenpp. + * + * Wrenpp is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Wrenpp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wrenpp. If not, see . + */ + +#include + +namespace wren { + typedef std::string_view wren_string_t; +} //namespace wren diff --git a/src/def_configuration.cpp b/src/def_configuration.cpp index 4ebb2db..ff18246 100644 --- a/src/def_configuration.cpp +++ b/src/def_configuration.cpp @@ -20,11 +20,11 @@ #include namespace wren { - void DefConfiguration::write_fn (VM*, const char* text) { + void DefConfiguration::write_fn (VM*, wren_string_t text) { std::cout << text; } - void DefConfiguration::error_fn (VM*, ErrorType type, const char* module, int line, const char* message) { + void DefConfiguration::error_fn (VM*, ErrorType type, wren_string_t module, int line, wren_string_t message) { std::cerr << "Wren error: " << message << " in " << module << ':' << line << '\n'; }