Pass string_view instead of const char* to callback functions
This commit is contained in:
parent
f84234cede
commit
6f4b0ce094
7 changed files with 63 additions and 42 deletions
|
@ -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<char*>(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<Calendar>();
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "error_type.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "StringCRC32.hpp"
|
||||
#include "wren_types.hpp"
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
@ -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 <typename T, typename F> struct AnyFunctionWrap;
|
||||
template <typename T, typename R, typename... Args>
|
||||
|
|
22
include/wrenpp/wren_types.hpp
Normal file
22
include/wrenpp/wren_types.hpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace wren {
|
||||
typedef std::string_view wren_string_t;
|
||||
} //namespace wren
|
|
@ -20,11 +20,11 @@
|
|||
#include <algorithm>
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue