Created
October 1, 2017 13:48
-
-
Save clarete/9579abbfd04f0bb8b060bd9f8e957fd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vm.hpp> | |
#include <bindgen.hpp> | |
#include <curl/curl.h> | |
static std::map<void*, oop> meme_mapping; | |
static int prim_curl_global_cleanup (Process* proc) { | |
/* Call to the underlying function */ | |
curl_global_cleanup (); | |
/* Push output to the stack */ | |
proc->stack_push(MM_NULL); | |
return 0; | |
} | |
static int prim_curl_global_init (Process* proc) { | |
/* Argument[0] */ | |
oop oop_flags = proc->get_arg(0); | |
long prim_flags; | |
if (proc->mmobj()->mm_object_vt(oop_flags) == proc->vm()->get_prime("LongNum")) { | |
prim_flags = proc->mmobj()->mm_longnum_get(proc, oop_flags); | |
} | |
else if (proc->mmobj()->mm_object_vt(oop_flags) == proc->vm()->get_prime("Integer")) { | |
prim_flags = untag_small_int(oop_flags); | |
} | |
else { | |
proc->raise("TypeError", "Argument `flags' expected LongNum or Integer"); | |
} | |
/* Call to the underlying function */ | |
int exc = 0; | |
CURLcode output = curl_global_init (prim_flags); | |
oop oop_output = tag_small_int(output); | |
/* Push output to the stack */ | |
proc->stack_push(oop_output); | |
return 0; | |
} | |
static int prim_curl_easy_init (Process* proc) { | |
/* Call to the underlying function */ | |
int exc = 0; | |
CURL * output = curl_easy_init (); | |
oop oop_output = meme_instance(proc, meme_mapping, "CURL", output, &exc); | |
/* Push output to the stack */ | |
proc->stack_push(oop_output); | |
return 0; | |
} | |
static int prim_curl_easy_cleanup (Process* proc) { | |
/* Argument[0] */ | |
oop oop_handle = proc->get_arg(0); | |
CURL * prim_handle; | |
if (proc->mmobj()->mm_object_vt(oop_handle) == proc->vm()->get_prime("Object")) { | |
prim_handle = (CURL *) ((oop *) oop_handle)[2]; | |
} | |
else { | |
proc->raise("TypeError", "Argument `handle' expected Object"); | |
} | |
/* Call to the underlying function */ | |
curl_easy_cleanup (prim_handle); | |
/* Push output to the stack */ | |
proc->stack_push(MM_NULL); | |
return 0; | |
} | |
static int prim_curl_easy_duphandle (Process* proc) { | |
/* Argument[0] */ | |
oop oop_handle = proc->get_arg(0); | |
CURL * prim_handle; | |
if (proc->mmobj()->mm_object_vt(oop_handle) == proc->vm()->get_prime("Object")) { | |
prim_handle = (CURL *) ((oop *) oop_handle)[2]; | |
} | |
else { | |
proc->raise("TypeError", "Argument `handle' expected Object"); | |
} | |
/* Call to the underlying function */ | |
int exc = 0; | |
CURL * output = curl_easy_duphandle (prim_handle); | |
oop oop_output = meme_instance(proc, meme_mapping, "CURL", output, &exc); | |
/* Push output to the stack */ | |
proc->stack_push(oop_output); | |
return 0; | |
} | |
static int prim_curl_easy_escape (Process* proc) { | |
/* Argument[0] */ | |
oop oop_curl = proc->get_arg(0); | |
CURL * prim_curl; | |
if (proc->mmobj()->mm_object_vt(oop_curl) == proc->vm()->get_prime("Object")) { | |
prim_curl = (CURL *) ((oop *) oop_curl)[2]; | |
} | |
else { | |
proc->raise("TypeError", "Argument `curl' expected Object"); | |
} | |
/* Argument[1] */ | |
oop oop_string = proc->get_arg(1); | |
const char * prim_string; | |
if (proc->mmobj()->mm_object_vt(oop_string) == proc->vm()->get_prime("String")) { | |
prim_string = proc->mmobj()->mm_string_cstr(proc, oop_string); | |
} | |
else { | |
proc->raise("TypeError", "Argument `string' expected String"); | |
} | |
/* Argument[2] */ | |
oop oop_length = proc->get_arg(2); | |
int prim_length; | |
if (proc->mmobj()->mm_object_vt(oop_length) == proc->vm()->get_prime("Integer")) { | |
prim_length = untag_small_int(oop_length); | |
} | |
else { | |
proc->raise("TypeError", "Argument `length' expected Integer"); | |
} | |
/* Call to the underlying function */ | |
int exc = 0; | |
char * output = curl_easy_escape (prim_curl, prim_string, prim_length); | |
oop oop_output = proc->mmobj()->mm_string_new(output); | |
/* Push output to the stack */ | |
proc->stack_push(oop_output); | |
return 0; | |
} | |
void init_primitives_curl (VM *vm) { | |
vm->register_primitive("curl_curl_global_cleanup", prim_curl_global_cleanup); | |
vm->register_primitive("curl_curl_global_init", prim_curl_global_init); | |
vm->register_primitive("curl_curl_easy_init", prim_curl_easy_init); | |
vm->register_primitive("curl_curl_easy_cleanup", prim_curl_easy_cleanup); | |
vm->register_primitive("curl_curl_easy_duphandle", prim_curl_easy_duphandle); | |
vm->register_primitive("curl_curl_easy_escape", prim_curl_easy_escape); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment