Skip to content

Commit

Permalink
got forign method working
Browse files Browse the repository at this point in the history
tristanisham committed Jan 23, 2024
1 parent f09bc7a commit 16a22f6
Showing 5 changed files with 16 additions and 128 deletions.
1 change: 1 addition & 0 deletions include/lib.hpp
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@ namespace net::http {

void method(WrenVM* vm);


}

namespace encoding {
25 changes: 4 additions & 21 deletions lang/net/http.fan
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
foreign class Request {
construct new(url) {
this._headers = {}
}

headers {_headers}

foreign method(method)


[key]=(value) {
this._headers[key] = value
}

[key] {
return this._headers[key]
}
}



foreign class Client {
construct new(url) {}
foreign method(m)
}
19 changes: 9 additions & 10 deletions src/vm/config.cpp
Original file line number Diff line number Diff line change
@@ -165,7 +165,7 @@ WrenLoadModuleResult loadModuleFn(WrenVM* vm, const char* name) {
for (const auto& seg : p) {
if (i == 0 && seg == "std") {
auto fan_install = std::getenv("FAN_LIB");
if (fan_install != NULL) {
if (fan_install != nullptr) {
searchPath.append(fan_install);
} else {
// lib::abort(vm, "standard library not found. Please set the environment variable: 'FAN_LIB'");
@@ -221,7 +221,7 @@ WrenForeignClassMethods bindForeignClassFn(WrenVM* vm, const char* module, const
}

if (strcmp(module, "std/net/http") == 0) {
if (strcmp(className, "Request") == 0) {
if (strcmp(className, "Client") == 0) {
methods.allocate = lib::net::http::requestAlloc;
methods.finalize = lib::net::http::requestDealloc;
return methods;
@@ -232,6 +232,13 @@ WrenForeignClassMethods bindForeignClassFn(WrenVM* vm, const char* module, const
}

WrenForeignMethodFn bindForeignMethodFn(WrenVM* vm, const char* module, const char* className, bool isStatic, const char* signature) {
if (strcmp(module, "std/net/http") == 0) {
if (strcmp(className, "Client") == 0) {
if (!isStatic && strcmp(signature, "method(_)") == 0) {
return lib::net::http::method;
}
}
}

if (strcmp(module, "std/fs") == 0) {
if (strcmp(className, "File") == 0) {
@@ -335,14 +342,6 @@ WrenForeignMethodFn bindForeignMethodFn(WrenVM* vm, const char* module, const ch
}
}

if (strcmp(module, "std/net/http") == 0) {
if (strcmp(className, "Request") == 0) {
if (!isStatic && strcmp(signature, "method(_)")) {
return lib::net::http::method;
}
}
}

if (std::strcmp(module, "std/encoding") == 0) {
if (std::strcmp(className, "Base64") == 0) {
if (isStatic && std::strcmp(signature, "encode(_)") == 0) {
94 changes: 0 additions & 94 deletions src/vm/std/net/http.cpp
Original file line number Diff line number Diff line change
@@ -16,26 +16,6 @@ size_t writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
return size * nmemb;
}

// static std::unordered_map<std::string, std::string> splitHeaders(const std::string& header_string) {
// std::istringstream stream(header_string);
// std::unordered_map<std::string, std::string> output;
// std::string line;

// while (std::getline(stream, line)) {
// auto place = line.find_first_of(':');
// if (place == std::string::npos) {
// continue;
// }
// auto key = line.substr(0, place);
// // Increment 'place' by 2 to skip over the ':' and the space after it
// auto val = (place + 1 < line.length() ? line.substr(place + 2) : "");
// key = trim(key);
// val = trim(val);
// output.emplace(key, val);
// }

// return output;
// }

void lib::net::http::requestAlloc(WrenVM* vm) {
try {
@@ -94,77 +74,3 @@ void lib::net::http::method(WrenVM* vm) {
curl_easy_setopt(*curl, CURLOPT_CUSTOMREQUEST, method_str.c_str());
}

// void lib::net::http::fetch(WrenVM* vm) {
// try {
// const char* raw_url = wrenGetSlotString(vm, 1);
// if (raw_url == nullptr) {
// lib::abort(vm, "Invalid memory slot for function");
// }

// auto optionalMap = wrenGetSlotType(vm, 2);
// if (optionalMap != WREN_TYPE_NULL) {
// if (optionalMap != WREN_TYPE_MAP) {
// lib::abort(vm, "This paramater must be null or a map");
// }

// // Handle a map

// }

// auto curl = curl_easy_init();
// if (!curl) {
// lib::abort(vm, "Unable to construct CURL request");
// }

// curl_easy_setopt(curl, CURLOPT_URL, raw_url);
// curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
// curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
// curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

// std::string response_string;
// std::string header_string;
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
// curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string);

// auto res = curl_easy_perform(curl);
// if (res != CURLE_OK) {
// lib::abort(vm, (boost::format("HTTP Request failed: %1%") % curl_easy_strerror(res)).str());
// }

// long response_code;
// double elapsed;
// curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
// curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);

// // Cleanup
// curl_easy_cleanup(curl);
// curl_global_cleanup();
// curl = NULL;

// // TODO: This code fucking sucks
// auto headers = splitHeaders(header_string);
// std::unordered_map<std::string, std::string> entries;

// for (const auto& [key, value] : headers) {
// entries[key] = value;
// }
// entries["elapsed"] = std::to_string(elapsed);
// entries["body"] = response_string;
// entries["status"] = std::to_string(response_code);
// // for (const auto& [key, val] : entries) {
// // std::visit([&](auto&& arg) { std::cerr << key << " : " << arg << std::endl; }, val);
// // }

// try {
// vm::createVmMap(vm, 0, entries);
// } catch (const std::logic_error&) {
// lib::abort(vm, "RUNTIME ERROR: Unable to generate correctly sized map with your succsessful result");
// return;
// }

// // If we get here then the connection is closed gracefully
// } catch (const std::exception& e) {
// lib::abort(vm, e.what());
// }
// }
5 changes: 2 additions & 3 deletions tests/http.fan
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "std/net/http" for Request
import "std/net/http" for Client

var resp = Request.fetch("https://example.com/")
System.print("%(resp)")
Client.new("https://example.com")

0 comments on commit 16a22f6

Please sign in to comment.