Commit 27c97fb5 authored by Michael Droettboom's avatar Michael Droettboom

Add documentation to hiwire

parent 5e9393a8
...@@ -61,7 +61,7 @@ EM_JS(int, hiwire_true, (), { return Module.hiwire_new_value(true); }); ...@@ -61,7 +61,7 @@ EM_JS(int, hiwire_true, (), { return Module.hiwire_new_value(true); });
EM_JS(int, hiwire_false, (), { return Module.hiwire_new_value(false); }); EM_JS(int, hiwire_false, (), { return Module.hiwire_new_value(false); });
EM_JS(int, hiwire_throw_error, (int idmsg), { EM_JS(void, hiwire_throw_error, (int idmsg), {
var jsmsg = Module.hiwire_get_value(idmsg); var jsmsg = Module.hiwire_get_value(idmsg);
Module.hiwire_decref(idmsg); Module.hiwire_decref(idmsg);
throw new Error(jsmsg); throw new Error(jsmsg);
...@@ -133,16 +133,16 @@ EM_JS(void, hiwire_new, (int idobj, int idargs), { ...@@ -133,16 +133,16 @@ EM_JS(void, hiwire_new, (int idobj, int idargs), {
return Module.hiwire_new_value(newCall.apply(newCall, jsargs)); return Module.hiwire_new_value(newCall.apply(newCall, jsargs));
}); });
EM_JS(void, hiwire_get_length, (int idobj), { EM_JS(int, hiwire_get_length, (int idobj), {
return Module.hiwire_get_value(idobj).length; return Module.hiwire_get_value(idobj).length;
}); });
EM_JS(void, hiwire_is_function, (int idobj), { EM_JS(int, hiwire_is_function, (int idobj), {
// clang-format off // clang-format off
return typeof Module.hiwire_get_value(idobj) === 'function'; return typeof Module.hiwire_get_value(idobj) === 'function';
// clang-format on // clang-format on
}); });
EM_JS(void, hiwire_to_string, (int idobj), { EM_JS(int, hiwire_to_string, (int idobj), {
return Module.hiwire_new_value(Module.hiwire_get_value(idobj).toString()); return Module.hiwire_new_value(Module.hiwire_get_value(idobj).toString());
}); });
#ifndef HIWIRE_H #ifndef HIWIRE_H
#define HIWIRE_H #define HIWIRE_H
// TODO: Document me /**
* hiwire: A super-simple framework for converting values between C and
* Javascript.
*
* Arbitrary Javascript objects are referenced from C using an opaque int value.
* By convention, these ids are stored in variable names beginning with `id`.
*
* Javascript objects passed to the C side must be manually reference-counted.
* Use `hiwire_incref` if you plan to store the object on the C side. Use
* `hiwire_decref` when done. Internally, the objects are stored in a global
* object. There may be one or more keys pointing to the same object.
*/
/**
* Initialize the variables and functions required for hiwire.
*/
void void
hiwire_setup(); hiwire_setup();
/**
* Increase the reference count on an object.
*
* Returns: The new reference
*/
int int
hiwire_incref(int idval); hiwire_incref(int idval);
/**
* Decrease the reference count on an object.
*/
void void
hiwire_decref(int idval); hiwire_decref(int idval);
/**
* Create a new Javascript integer with the given value.
*
* Returns: New reference
*/
int int
hiwire_int(int val); hiwire_int(int val);
/**
* Create a new Javascript float with the given value.
*
* Returns: New reference
*/
int int
hiwire_double(double val); hiwire_double(double val);
/**
* Create a new Javascript string, given a pointer to a buffer containing UTF8
* and a length, in bytes. The string data itself is copied.
*
* Returns: New reference
*/
int int
hiwire_string_utf8_length(int ptr, int len); hiwire_string_utf8_length(int ptr, int len);
/**
* Create a new Javascript string, given a pointer to a null-terminated buffer
* containing UTF8. The string data itself is copied.
*
* Returns: New reference
*/
int int
hiwire_string_utf8(int ptr); hiwire_string_utf8(int ptr);
/**
* Create a new Javascript Uint8ClampedArray, given a pointer to a buffer and a
* length, in bytes.
*
* The array's data is not copied.
*
* Returns: New reference
*/
int int
hiwire_bytes(int ptr, int len); hiwire_bytes(int ptr, int len);
/**
* Create a new Javascript undefined value.
*
* Returns: New reference
*/
int int
hiwire_undefined(); hiwire_undefined();
/**
* Create a new Javascript null value.
*
* Returns: New reference
*/
int int
hiwire_null(); hiwire_null();
/**
* Create a new Javascript true value.
*
* Returns: New reference
*/
int int
hiwire_true(); hiwire_true();
/**
* Create a new Javascript false value.
*
* Returns: New reference
*/
int int
hiwire_false(); hiwire_false();
/**
* Create a new Javascript Array.
*
* Returns: New reference
*/
int int
hiwire_array(); hiwire_array();
int /**
* Push a value to the end of a Javascript array.
*
* If the user no longer needs the value outside of the array, it is the user's
* responsibility to decref it.
*/
void
hiwire_push_array(int idobj, int idval); hiwire_push_array(int idobj, int idval);
/**
* Create a new Javascript object.
*
* Returns: New reference
*/
int int
hiwire_object(); hiwire_object();
int /**
* Add a new key/value pair to a Javascript object.
*
* If the user no longer needs the key or value outside of the object, it is the
* user's responsibility to decref them.
*/
void
hiwire_push_object_pair(int idobj, int idkey, int idval); hiwire_push_object_pair(int idobj, int idkey, int idval);
int /**
* Throws a new Error object with the given message.
*
* The message is conventionally a Javascript string, but that is not required.
*/
void
hiwire_throw_error(int idmsg); hiwire_throw_error(int idmsg);
/**
* Get a Javascript object from the global namespace, i.e. window.
*
* Returns: New reference
*/
int int
hiwire_get_global(int ptrname); hiwire_get_global(int ptrname);
/**
* Get an object member by string.
*
* The string is a char* to null-terminated UTF8.
*
* Returns: New reference
*/
int int
hiwire_get_member_string(int idobj, int ptrname); hiwire_get_member_string(int idobj, int ptrname);
/**
* Set an object member by string.
*
* The string is a char* to null-terminated UTF8.
*/
void void
hiwire_set_member_string(int idobj, int ptrname, int idval); hiwire_set_member_string(int idobj, int ptrname, int idval);
/**
* Get an object member by integer.
*
* The integer is a C integer, not an id reference to a Javascript integer.
*
* Returns: New reference
*/
int int
hiwire_get_member_int(int idobj, int idx); hiwire_get_member_int(int idobj, int idx);
/**
* Set an object member by integer.
*
* The integer is a C integer, not an id reference to a Javascript integer.
*
* Returns: New reference
*/
void void
hiwire_set_member_int(int idobj, int idx, int idval); hiwire_set_member_int(int idobj, int idx, int idval);
/**
* Call a function
*
* idargs is a hiwire Array containing the arguments.
*
* Returns: New reference
*/
int int
hiwire_call(int idobj, int idargs); hiwire_call(int idobj, int idargs);
/**
* Call a member function.
*
* ptrname is the member name, as a char * to null-terminated UTF8.
*
* idargs is a hiwire Array containing the arguments.
*
* Returns: New reference
*/
int int
hiwire_call_member(int idobj, int ptrname, int idargs); hiwire_call_member(int idobj, int ptrname, int idargs);
/**
* Calls the constructor of a class object.
*
* idargs is a hiwire Array containing the arguments.
*
* Returns: New reference
*/
int int
hiwire_new(int idobj, int idargs); hiwire_new(int idobj, int idargs);
/**
* Returns the value of the `length` member on a Javascript object.
*
* Returns: C int
*/
int int
hiwire_get_length(int idobj); hiwire_get_length(int idobj);
/**
* Returns 1 if the object is a function.
*
* Returns: C int
*/
int int
hiwire_is_function(int idobj); hiwire_is_function(int idobj);
/**
* Gets the string representation of an object by calling `toString`.
*
* Returns: New reference to Javascript string
*/
int int
hiwire_to_string(int idobj); hiwire_to_string(int idobj);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment