Rework the public API (#4829)

Related to #4186.

Some notable changes:
  - The term 'Error' now strictly refers to native Error objects defined in
    the ECMA standard, which are ordinary objects. All other uses of
    'error' or 'error reference' where the term refers to a thrown value is
    now called 'exception'.

  - Simplified the naming scheme of many String API functions. These functions
    will now also take an 'encoding' argument to specify the desired
    encoding in which to operate.

  - Removed the substring-copy-to-buffer functions. These functions
    behaved awkwardly, as they use character index to specify the
    start/end positions, and were mostly used incorrectly with byte
    offsets instead. The functionality can still be replicated with
    other functions if necessary.

  - String-to-buffer functions will no longer fail if the buffer is not
    sufficiently large, the string will instead be cropped.

  - Fixed the usage of the '_sz' prefix in many API functions. The term
    'sz' means zero-terminated string in hungarian notation, this was
    used incorrectly in many cases.

  - Renamed most of the public API functions to have shorter, more on-point
    names, rather than the often too long descriptive names. Functions are now
    also grouped by the type of value they operate on, where this makes
    sense.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2021-12-06 10:20:09 +01:00
committed by GitHub
parent 81d2319144
commit 9860d66a56
180 changed files with 10738 additions and 11025 deletions
@@ -12,9 +12,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "jerryscript-mbed-util/logging.h"
#include "mbed.h"
/**
@@ -22,9 +21,11 @@
*
* Called if/when the AnalogIn is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(AnalogIn)(void* void_ptr, jerry_object_native_info_t *info_p) {
(void) info_p;
delete static_cast<AnalogIn*>(void_ptr);
void
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (AnalogIn) (void* void_ptr, jerry_object_native_info_t* info_p)
{
(void) info_p;
delete static_cast<AnalogIn*> (void_ptr);
}
/**
@@ -32,9 +33,8 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(AnalogIn)(void* void_ptr, jerry_object_nat
*
* Set AnalogIn#destructor as the free callback.
*/
static const jerry_object_native_info_t native_obj_type_info = {
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(AnalogIn)
};
static const jerry_object_native_info_t native_obj_type_info = { .free_cb =
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (AnalogIn) };
/**
* AnalogIn#read (native JavaScript method)
@@ -43,22 +43,22 @@ static const jerry_object_native_info_t native_obj_type_info = {
*
* @returns A floating-point value representing the current input voltage, measured as a percentage
*/
DECLARE_CLASS_FUNCTION(AnalogIn, read) {
CHECK_ARGUMENT_COUNT(AnalogIn, read, (args_count == 0));
DECLARE_CLASS_FUNCTION (AnalogIn, read)
{
CHECK_ARGUMENT_COUNT (AnalogIn, read, (args_count == 0));
// Extract native AnalogIn pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native AnalogIn pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native AnalogIn pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native AnalogIn pointer");
}
AnalogIn* native_ptr = static_cast<AnalogIn*>(void_ptr);
AnalogIn* native_ptr = static_cast<AnalogIn*> (void_ptr);
float result = native_ptr->read();
return jerry_create_number(result);
float result = native_ptr->read ();
return jerry_number (result);
}
/**
@@ -68,22 +68,22 @@ DECLARE_CLASS_FUNCTION(AnalogIn, read) {
*
* @returns 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
*/
DECLARE_CLASS_FUNCTION(AnalogIn, read_u16) {
CHECK_ARGUMENT_COUNT(AnalogIn, read_u16, (args_count == 0));
DECLARE_CLASS_FUNCTION (AnalogIn, read_u16)
{
CHECK_ARGUMENT_COUNT (AnalogIn, read_u16, (args_count == 0));
// Extract native AnalogIn pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native AnalogIn pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native AnalogIn pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native AnalogIn pointer");
}
AnalogIn* native_ptr = static_cast<AnalogIn*>(void_ptr);
AnalogIn* native_ptr = static_cast<AnalogIn*> (void_ptr);
uint16_t result = native_ptr->read_u16();
return jerry_create_number(result);
uint16_t result = native_ptr->read_u16 ();
return jerry_number (result);
}
/**
@@ -92,22 +92,23 @@ DECLARE_CLASS_FUNCTION(AnalogIn, read_u16) {
* @param pin_name mbed pin to connect the AnalogIn to.
* @returns a JavaScript object representing a AnalogIn.
*/
DECLARE_CLASS_CONSTRUCTOR(AnalogIn) {
CHECK_ARGUMENT_COUNT(AnalogIn, __constructor, args_count == 1);
CHECK_ARGUMENT_TYPE_ALWAYS(AnalogIn, __constructor, 0, number);
DECLARE_CLASS_CONSTRUCTOR (AnalogIn)
{
CHECK_ARGUMENT_COUNT (AnalogIn, __constructor, args_count == 1);
CHECK_ARGUMENT_TYPE_ALWAYS (AnalogIn, __constructor, 0, number);
PinName pin_name = PinName(jerry_get_number_value(args[0]));
PinName pin_name = PinName (jerry_value_as_number (args[0]));
// create native object
AnalogIn* native_ptr = new AnalogIn(pin_name);
// create native object
AnalogIn* native_ptr = new AnalogIn (pin_name);
// create the jerryscript object
jerry_value_t js_object = jerry_create_object();
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
// create the jerryscript object
jerry_value_t js_object = jerry_object ();
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
// attach methods
ATTACH_CLASS_FUNCTION(js_object, AnalogIn, read);
ATTACH_CLASS_FUNCTION(js_object, AnalogIn, read_u16);
// attach methods
ATTACH_CLASS_FUNCTION (js_object, AnalogIn, read);
ATTACH_CLASS_FUNCTION (js_object, AnalogIn, read_u16);
return js_object;
return js_object;
}
@@ -12,9 +12,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "jerryscript-mbed-util/logging.h"
#include "mbed.h"
/**
@@ -22,9 +21,11 @@
*
* Called if/when the DigitalOut is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)(void* void_ptr, jerry_object_native_info_t *info_p) {
(void) info_p;
delete static_cast<DigitalOut*>(void_ptr);
void
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (DigitalOut) (void* void_ptr, jerry_object_native_info_t* info_p)
{
(void) info_p;
delete static_cast<DigitalOut*> (void_ptr);
}
/**
@@ -32,9 +33,8 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)(void* void_ptr, jerry_object_n
*
* Set DigitalOut#destructor as the free callback.
*/
static const jerry_object_native_info_t native_obj_type_info = {
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)
};
static const jerry_object_native_info_t native_obj_type_info = { .free_cb =
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (DigitalOut) };
/**
* DigitalOut#write (native JavaScript method)
@@ -45,25 +45,25 @@ static const jerry_object_native_info_t native_obj_type_info = {
* respectively
* @returns undefined, or an error if invalid arguments are provided.
*/
DECLARE_CLASS_FUNCTION(DigitalOut, write) {
CHECK_ARGUMENT_COUNT(DigitalOut, write, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(DigitalOut, write, 0, number);
DECLARE_CLASS_FUNCTION (DigitalOut, write)
{
CHECK_ARGUMENT_COUNT (DigitalOut, write, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (DigitalOut, write, 0, number);
// Extract native DigitalOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native DigitalOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native DigitalOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native DigitalOut pointer");
}
DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
DigitalOut* native_ptr = static_cast<DigitalOut*> (void_ptr);
int arg0 = jerry_get_number_value(args[0]);
native_ptr->write(arg0);
int arg0 = jerry_value_as_number (args[0]);
native_ptr->write (arg0);
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -73,22 +73,22 @@ DECLARE_CLASS_FUNCTION(DigitalOut, write) {
*
* @returns 1 if the pin is currently high, or 0 if the pin is currently low.
*/
DECLARE_CLASS_FUNCTION(DigitalOut, read) {
CHECK_ARGUMENT_COUNT(DigitalOut, read, (args_count == 0));
DECLARE_CLASS_FUNCTION (DigitalOut, read)
{
CHECK_ARGUMENT_COUNT (DigitalOut, read, (args_count == 0));
// Extract native DigitalOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native DigitalOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native DigitalOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native DigitalOut pointer");
}
DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
DigitalOut* native_ptr = static_cast<DigitalOut*> (void_ptr);
int result = native_ptr->read();
return jerry_create_number(result);
int result = native_ptr->read ();
return jerry_number (result);
}
/**
@@ -97,22 +97,22 @@ DECLARE_CLASS_FUNCTION(DigitalOut, read) {
* @returns 0 if the DigitalOut is set to NC, or 1 if it is connected to an
* actual pin
*/
DECLARE_CLASS_FUNCTION(DigitalOut, is_connected) {
CHECK_ARGUMENT_COUNT(DigitalOut, is_connected, (args_count == 0));
DECLARE_CLASS_FUNCTION (DigitalOut, is_connected)
{
CHECK_ARGUMENT_COUNT (DigitalOut, is_connected, (args_count == 0));
// Extract native DigitalOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native DigitalOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native DigitalOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native DigitalOut pointer");
}
DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
DigitalOut* native_ptr = static_cast<DigitalOut*> (void_ptr);
int result = native_ptr->is_connected();
return jerry_create_number(result);
int result = native_ptr->is_connected ();
return jerry_number (result);
}
/**
@@ -122,35 +122,37 @@ DECLARE_CLASS_FUNCTION(DigitalOut, is_connected) {
* @param value (optional) Initial value of the DigitalOut.
* @returns a JavaScript object representing a DigitalOut.
*/
DECLARE_CLASS_CONSTRUCTOR(DigitalOut) {
CHECK_ARGUMENT_COUNT(DigitalOut, __constructor, (args_count == 1 || args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS(DigitalOut, __constructor, 0, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION(DigitalOut, __constructor, 1, number, (args_count == 2));
DECLARE_CLASS_CONSTRUCTOR (DigitalOut)
{
CHECK_ARGUMENT_COUNT (DigitalOut, __constructor, (args_count == 1 || args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS (DigitalOut, __constructor, 0, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION (DigitalOut, __constructor, 1, number, (args_count == 2));
DigitalOut* native_ptr;
DigitalOut* native_ptr;
// Call correct overload of DigitalOut::DigitalOut depending on the
// arguments passed.
PinName pin_name = PinName(jerry_get_number_value(args[0]));
// Call correct overload of DigitalOut::DigitalOut depending on the
// arguments passed.
PinName pin_name = PinName (jerry_value_as_number (args[0]));
switch (args_count) {
case 1:
native_ptr = new DigitalOut(pin_name);
break;
case 2:
int value = static_cast<int>(jerry_get_number_value(args[1]));
native_ptr = new DigitalOut(pin_name, value);
break;
}
switch (args_count)
{
case 1:
native_ptr = new DigitalOut (pin_name);
break;
case 2:
int value = static_cast<int> (jerry_value_as_number (args[1]));
native_ptr = new DigitalOut (pin_name, value);
break;
}
// create the jerryscript object
jerry_value_t js_object = jerry_create_object();
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
// create the jerryscript object
jerry_value_t js_object = jerry_object ();
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
// attach methods
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, write);
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, read);
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, is_connected);
// attach methods
ATTACH_CLASS_FUNCTION (js_object, DigitalOut, write);
ATTACH_CLASS_FUNCTION (js_object, DigitalOut, read);
ATTACH_CLASS_FUNCTION (js_object, DigitalOut, is_connected);
return js_object;
return js_object;
}
@@ -12,10 +12,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-drivers/I2C-js.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "jerryscript-mbed-util/logging.h"
#include "mbed.h"
/**
@@ -23,9 +23,11 @@
*
* Called if/when the I2C object is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(I2C) (void *void_ptr, jerry_object_native_info_t *info_p) {
(void) info_p;
delete static_cast<I2C*>(void_ptr);
void
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (I2C) (void *void_ptr, jerry_object_native_info_t *info_p)
{
(void) info_p;
delete static_cast<I2C *> (void_ptr);
}
/**
@@ -33,9 +35,7 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(I2C) (void *void_ptr, jerry_object_native_
*
* Set I2C#destructor as the free callback.
*/
static const jerry_object_native_info_t native_obj_type_info = {
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(I2C)
};
static const jerry_object_native_info_t native_obj_type_info = { .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR (I2C) };
/**
* I2C#frequency (native JavaScript method)
@@ -44,25 +44,25 @@ static const jerry_object_native_info_t native_obj_type_info = {
*
* @param frequency New I2C Frequency
*/
DECLARE_CLASS_FUNCTION(I2C, frequency) {
CHECK_ARGUMENT_COUNT(I2C, frequency, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, frequency, 0, number);
DECLARE_CLASS_FUNCTION (I2C, frequency)
{
CHECK_ARGUMENT_COUNT (I2C, frequency, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, frequency, 0, number);
// Unwrap native I2C object
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Unwrap native I2C object
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native I2C pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C*>(void_ptr);
I2C *native_ptr = static_cast<I2C *> (void_ptr);
int hz = jerry_get_number_value(args[0]);
native_ptr->frequency(hz);
int hz = jerry_value_as_number (args[0]);
native_ptr->frequency (hz);
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -86,77 +86,84 @@ DECLARE_CLASS_FUNCTION(I2C, frequency) {
*
* @returns array: Data read from the I2C bus
*/
DECLARE_CLASS_FUNCTION(I2C, read) {
CHECK_ARGUMENT_COUNT(I2C, read, (args_count == 1 || args_count == 3 || args_count == 4));
DECLARE_CLASS_FUNCTION (I2C, read)
{
CHECK_ARGUMENT_COUNT (I2C, read, (args_count == 1 || args_count == 3 || args_count == 4));
if (args_count == 1) {
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 0, number);
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
if (args_count == 1)
{
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, read, 0, number);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C*>(void_ptr);
int data = jerry_get_number_value(args[0]);
int result = native_ptr->read(data);
return jerry_create_number(result);
} else {
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 1, array);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 2, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION(I2C, read, 3, boolean, (args_count == 4));
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C*>(void_ptr);
const uint32_t data_len = jerry_get_array_length(args[1]);
int address = jerry_get_number_value(args[0]);
int length = jerry_get_number_value(args[2]);
char *data = new char[data_len];
bool repeated = false;
if (args_count == 4) {
repeated = jerry_value_is_true(args[3]);
}
int result = native_ptr->read(address, data, length, repeated);
jerry_value_t out_array = jerry_create_array(data_len);
for (uint32_t i = 0; i < data_len; i++) {
jerry_value_t val = jerry_create_number(double(data[i]));
jerry_release_value(jerry_set_property_by_index(out_array, i, val));
jerry_release_value(val);
}
delete[] data;
if (result == 0) {
// ACK
return out_array;
} else {
// NACK
const char *error_msg = "NACK received from I2C bus";
jerry_release_value(out_array);
return jerry_create_error(JERRY_ERROR_COMMON, reinterpret_cast<const jerry_char_t *>(error_msg));
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C *> (void_ptr);
int data = jerry_value_as_number (args[0]);
int result = native_ptr->read (data);
return jerry_number (result);
}
else
{
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, read, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, read, 1, array);
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, read, 2, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION (I2C, read, 3, boolean, (args_count == 4));
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C *> (void_ptr);
const uint32_t data_len = jerry_array_length (args[1]);
int address = jerry_value_as_number (args[0]);
int length = jerry_value_as_number (args[2]);
char *data = new char[data_len];
bool repeated = false;
if (args_count == 4)
{
repeated = jerry_value_is_true (args[3]);
}
int result = native_ptr->read (address, data, length, repeated);
jerry_value_t out_array = jerry_array (data_len);
for (uint32_t i = 0; i < data_len; i++)
{
jerry_value_t val = jerry_number (double (data[i]));
jerry_value_free (jerry_object_set_index (out_array, i, val));
jerry_value_free (val);
}
delete[] data;
if (result == 0)
{
// ACK
return out_array;
}
else
{
// NACK
const char *error_msg = "NACK received from I2C bus";
jerry_value_free (out_array);
return jerry_throw_sz (JERRY_ERROR_COMMON, error_msg);
}
}
}
/**
@@ -179,65 +186,68 @@ DECLARE_CLASS_FUNCTION(I2C, read) {
*
* @returns 0 on success, non-0 on failure
*/
DECLARE_CLASS_FUNCTION(I2C, write) {
CHECK_ARGUMENT_COUNT(I2C, write, (args_count == 1 || args_count == 3 || args_count == 4));
DECLARE_CLASS_FUNCTION (I2C, write)
{
CHECK_ARGUMENT_COUNT (I2C, write, (args_count == 1 || args_count == 3 || args_count == 4));
if (args_count == 1) {
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 0, number);
if (args_count == 1)
{
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, write, 0, number);
// Extract native I2C object
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native I2C object
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C*>(void_ptr);
// Unwrap arguments
int data = jerry_get_number_value(args[0]);
int result = native_ptr->write(data);
return jerry_create_number(result);
} else {
// 3 or 4
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 1, array);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 2, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION(I2C, write, 3, boolean, (args_count == 4));
// Extract native I2C object
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C*>(void_ptr);
// Unwrap arguments
int address = jerry_get_number_value(args[0]);
const uint32_t data_len = jerry_get_array_length(args[1]);
int length = jerry_get_number_value(args[2]);
bool repeated = args_count == 4 && jerry_value_is_true(args[3]);
// Construct data byte array
char *data = new char[data_len];
for (uint32_t i = 0; i < data_len; i++) {
data[i] = jerry_get_number_value(jerry_get_property_by_index(args[1], i));
}
int result = native_ptr->write(address, data, length, repeated);
// free dynamically allocated resources
delete[] data;
return jerry_create_number(result);
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C *> (void_ptr);
// Unwrap arguments
int data = jerry_value_as_number (args[0]);
int result = native_ptr->write (data);
return jerry_number (result);
}
else
{
// 3 or 4
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, write, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, write, 1, array);
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, write, 2, number);
CHECK_ARGUMENT_TYPE_ON_CONDITION (I2C, write, 3, boolean, (args_count == 4));
// Extract native I2C object
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (void_ptr != NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C *> (void_ptr);
// Unwrap arguments
int address = jerry_value_as_number (args[0]);
const uint32_t data_len = jerry_array_length (args[1]);
int length = jerry_value_as_number (args[2]);
bool repeated = args_count == 4 && jerry_value_is_true (args[3]);
// Construct data byte array
char *data = new char[data_len];
for (uint32_t i = 0; i < data_len; i++)
{
data[i] = jerry_value_as_number (jerry_object_get_index (args[1], i));
}
int result = native_ptr->write (address, data, length, repeated);
// free dynamically allocated resources
delete[] data;
return jerry_number (result);
}
}
/**
@@ -245,22 +255,22 @@ DECLARE_CLASS_FUNCTION(I2C, write) {
*
* Creates a start condition on the I2C bus.
*/
DECLARE_CLASS_FUNCTION(I2C, start) {
CHECK_ARGUMENT_COUNT(I2C, start, (args_count == 0));
DECLARE_CLASS_FUNCTION (I2C, start)
{
CHECK_ARGUMENT_COUNT (I2C, start, (args_count == 0));
// Extract native I2C object
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native I2C object
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native I2C pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C*>(void_ptr);
I2C *native_ptr = static_cast<I2C *> (void_ptr);
native_ptr->start();
return jerry_create_undefined();
native_ptr->start ();
return jerry_undefined ();
}
/**
@@ -268,22 +278,22 @@ DECLARE_CLASS_FUNCTION(I2C, start) {
*
* Creates a stop condition on the I2C bus.
*/
DECLARE_CLASS_FUNCTION(I2C, stop) {
CHECK_ARGUMENT_COUNT(I2C, stop, (args_count == 0));
DECLARE_CLASS_FUNCTION (I2C, stop)
{
CHECK_ARGUMENT_COUNT (I2C, stop, (args_count == 0));
// Extract native I2C object
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native I2C object
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native I2C pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
}
I2C *native_ptr = static_cast<I2C*>(void_ptr);
I2C *native_ptr = static_cast<I2C *> (void_ptr);
native_ptr->stop();
return jerry_create_undefined();
native_ptr->stop ();
return jerry_undefined ();
}
/**
@@ -293,24 +303,25 @@ DECLARE_CLASS_FUNCTION(I2C, stop) {
* @param scl mbed pin for I2C clock
* @returns a JavaScript object representing the I2C bus.
*/
DECLARE_CLASS_CONSTRUCTOR(I2C) {
CHECK_ARGUMENT_COUNT(I2C, __constructor, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, __constructor, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, __constructor, 1, number);
DECLARE_CLASS_CONSTRUCTOR (I2C)
{
CHECK_ARGUMENT_COUNT (I2C, __constructor, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, __constructor, 0, number);
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, __constructor, 1, number);
int sda = jerry_get_number_value(args[0]);
int scl = jerry_get_number_value(args[1]);
int sda = jerry_value_as_number (args[0]);
int scl = jerry_value_as_number (args[1]);
I2C *native_ptr = new I2C((PinName)sda, (PinName)scl);
I2C *native_ptr = new I2C ((PinName) sda, (PinName) scl);
jerry_value_t js_object = jerry_create_object();
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
jerry_value_t js_object = jerry_object ();
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
ATTACH_CLASS_FUNCTION(js_object, I2C, frequency);
ATTACH_CLASS_FUNCTION(js_object, I2C, read);
ATTACH_CLASS_FUNCTION(js_object, I2C, write);
ATTACH_CLASS_FUNCTION(js_object, I2C, start);
ATTACH_CLASS_FUNCTION(js_object, I2C, stop);
ATTACH_CLASS_FUNCTION (js_object, I2C, frequency);
ATTACH_CLASS_FUNCTION (js_object, I2C, read);
ATTACH_CLASS_FUNCTION (js_object, I2C, write);
ATTACH_CLASS_FUNCTION (js_object, I2C, start);
ATTACH_CLASS_FUNCTION (js_object, I2C, stop);
return js_object;
return js_object;
}
@@ -12,10 +12,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-event-loop/EventLoop.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "jerryscript-mbed-util/logging.h"
#include "mbed.h"
/**
@@ -23,13 +22,15 @@
*
* Called if/when the InterruptIn object is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn) (void *void_ptr, jerry_object_native_info_t *info_p) {
(void) info_p;
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
void
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (InterruptIn) (void *void_ptr, jerry_object_native_info_t *info_p)
{
(void) info_p;
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
native_ptr->rise(0);
native_ptr->fall(0);
delete native_ptr;
native_ptr->rise (0);
native_ptr->fall (0);
delete native_ptr;
}
/**
@@ -37,9 +38,8 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn) (void *void_ptr, jerry_object
*
* Set InterruptIn#destructor as the free callback.
*/
static const jerry_object_native_info_t native_obj_type_info = {
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn)
};
static const jerry_object_native_info_t native_obj_type_info = { .free_cb =
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (InterruptIn) };
/**
* InterruptIn#rise (native JavaScript method)
@@ -48,62 +48,63 @@ static const jerry_object_native_info_t native_obj_type_info = {
*
* @param cb Callback function, or null to detach previously attached callback.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, rise) {
CHECK_ARGUMENT_COUNT(InterruptIn, rise, (args_count == 1));
DECLARE_CLASS_FUNCTION (InterruptIn, rise)
{
CHECK_ARGUMENT_COUNT (InterruptIn, rise, (args_count == 1));
// Detach the rise callback when InterruptIn::rise(null) is called
if (jerry_value_is_null(args[0])) {
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Detach the rise callback when InterruptIn::rise(null) is called
if (jerry_value_is_null (args[0]))
{
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_rise");
jerry_value_t cb_func = jerry_get_property(call_info_p->this_value, property_name);
jerry_release_value(property_name);
// Only drop the callback if it exists
if (jerry_value_is_function(cb_func)) {
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance().dropCallback(cb_func);
}
jerry_release_value(cb_func);
native_ptr->rise(0);
return jerry_create_undefined();
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, rise, 0, function);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
jerry_value_t property_name = jerry_string_sz ("cb_rise");
jerry_value_t cb_func = jerry_object_get (call_info_p->this_value, property_name);
jerry_value_free (property_name);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
// Only drop the callback if it exists
if (jerry_value_is_function (cb_func))
{
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance ().dropCallback (cb_func);
}
jerry_value_free (cb_func);
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
native_ptr->rise (0);
jerry_value_t f = args[0];
return jerry_undefined ();
}
// Pass the function to EventLoop.
mbed::Callback<void()> cb = mbed::js::EventLoop::getInstance().wrapFunction(f);
native_ptr->rise(cb);
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, rise, 0, function);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_rise");
jerry_release_value(jerry_set_property(call_info_p->this_value, property_name, f));
jerry_release_value(property_name);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
return jerry_create_undefined();
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
jerry_value_t f = args[0];
// Pass the function to EventLoop.
mbed::Callback<void ()> cb = mbed::js::EventLoop::getInstance ().wrapFunction (f);
native_ptr->rise (cb);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_string_sz ("cb_rise");
jerry_value_free (jerry_object_set (call_info_p->this_value, property_name, f));
jerry_value_free (property_name);
return jerry_undefined ();
}
/**
@@ -113,62 +114,63 @@ DECLARE_CLASS_FUNCTION(InterruptIn, rise) {
*
* @param cb Callback function, or null to detach previously attached callback.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, fall) {
CHECK_ARGUMENT_COUNT(InterruptIn, fall, (args_count == 1));
DECLARE_CLASS_FUNCTION (InterruptIn, fall)
{
CHECK_ARGUMENT_COUNT (InterruptIn, fall, (args_count == 1));
// Detach the fall callback when InterruptIn::fall(null) is called
if (jerry_value_is_null(args[0])) {
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Detach the fall callback when InterruptIn::fall(null) is called
if (jerry_value_is_null (args[0]))
{
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_fall");
jerry_value_t cb_func = jerry_get_property(call_info_p->this_value, property_name);
jerry_release_value(property_name);
// Only drop the callback if it exists
if (jerry_value_is_function(cb_func)) {
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance().dropCallback(cb_func);
}
jerry_release_value(cb_func);
native_ptr->fall(0);
return jerry_create_undefined();
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, fall, 0, function);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
jerry_value_t property_name = jerry_string_sz ("cb_fall");
jerry_value_t cb_func = jerry_object_get (call_info_p->this_value, property_name);
jerry_value_free (property_name);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
// Only drop the callback if it exists
if (jerry_value_is_function (cb_func))
{
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance ().dropCallback (cb_func);
}
jerry_value_free (cb_func);
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
native_ptr->fall (0);
jerry_value_t f = args[0];
return jerry_undefined ();
}
// Pass the function to EventLoop.
mbed::Callback<void()> cb = mbed::js::EventLoop::getInstance().wrapFunction(f);
native_ptr->fall(cb);
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, fall, 0, function);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_fall");
jerry_release_value(jerry_set_property(call_info_p->this_value, property_name, f));
jerry_release_value(property_name);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
return jerry_create_undefined();
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
jerry_value_t f = args[0];
// Pass the function to EventLoop.
mbed::Callback<void ()> cb = mbed::js::EventLoop::getInstance ().wrapFunction (f);
native_ptr->fall (cb);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_string_sz ("cb_fall");
jerry_value_free (jerry_object_set (call_info_p->this_value, property_name, f));
jerry_value_free (property_name);
return jerry_undefined ();
}
/**
@@ -178,24 +180,24 @@ DECLARE_CLASS_FUNCTION(InterruptIn, fall) {
*
* @param mode PullUp, PullDown, PullNone
*/
DECLARE_CLASS_FUNCTION(InterruptIn, mode) {
CHECK_ARGUMENT_COUNT(InterruptIn, mode, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, mode, 0, number);
DECLARE_CLASS_FUNCTION (InterruptIn, mode)
{
CHECK_ARGUMENT_COUNT (InterruptIn, mode, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, mode, 0, number);
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
int pull = jerry_get_number_value(args[0]);
native_ptr->mode((PinMode)pull);
int pull = jerry_value_as_number (args[0]);
native_ptr->mode ((PinMode) pull);
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -203,21 +205,21 @@ DECLARE_CLASS_FUNCTION(InterruptIn, mode) {
*
* Disable IRQ. See InterruptIn.h in mbed-os sources for more details.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, disable_irq) {
CHECK_ARGUMENT_COUNT(InterruptIn, disable_irq, (args_count == 0));
DECLARE_CLASS_FUNCTION (InterruptIn, disable_irq)
{
CHECK_ARGUMENT_COUNT (InterruptIn, disable_irq, (args_count == 0));
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
native_ptr->disable_irq();
return jerry_create_undefined();
native_ptr->disable_irq ();
return jerry_undefined ();
}
/**
@@ -225,21 +227,21 @@ DECLARE_CLASS_FUNCTION(InterruptIn, disable_irq) {
*
* Enable IRQ. See InterruptIn.h in mbed-os sources for more details.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, enable_irq) {
CHECK_ARGUMENT_COUNT(InterruptIn, enable_irq, (args_count == 0));
DECLARE_CLASS_FUNCTION (InterruptIn, enable_irq)
{
CHECK_ARGUMENT_COUNT (InterruptIn, enable_irq, (args_count == 0));
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
native_ptr->enable_irq();
return jerry_create_undefined();
native_ptr->enable_irq ();
return jerry_undefined ();
}
/**
@@ -249,21 +251,22 @@ DECLARE_CLASS_FUNCTION(InterruptIn, enable_irq) {
*
* @returns JavaScript object wrapping InterruptIn native object
*/
DECLARE_CLASS_CONSTRUCTOR(InterruptIn) {
CHECK_ARGUMENT_COUNT(InterruptIn, __constructor, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, __constructor, 0, number);
int pin = jerry_get_number_value(args[0]);
DECLARE_CLASS_CONSTRUCTOR (InterruptIn)
{
CHECK_ARGUMENT_COUNT (InterruptIn, __constructor, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, __constructor, 0, number);
int pin = jerry_value_as_number (args[0]);
InterruptIn *native_ptr = new InterruptIn((PinName)pin);
jerry_value_t js_object = jerry_create_object();
InterruptIn *native_ptr = new InterruptIn ((PinName) pin);
jerry_value_t js_object = jerry_object ();
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, rise);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, fall);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, mode);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, enable_irq);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, disable_irq);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, rise);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, fall);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, mode);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, enable_irq);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, disable_irq);
return js_object;
return js_object;
}
@@ -12,9 +12,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "jerryscript-mbed-util/logging.h"
#include "mbed.h"
/**
@@ -22,9 +21,11 @@
*
* Called if/when the PwmOut is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)(void* void_ptr, jerry_object_native_info_t *info_p) {
(void) info_p;
delete static_cast<PwmOut*>(void_ptr);
void
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (PwmOut) (void* void_ptr, jerry_object_native_info_t* info_p)
{
(void) info_p;
delete static_cast<PwmOut*> (void_ptr);
}
/**
@@ -32,9 +33,7 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)(void* void_ptr, jerry_object_nativ
*
* Set PwmOut#destructor as the free callback.
*/
static const jerry_object_native_info_t native_obj_type_info = {
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)
};
static const jerry_object_native_info_t native_obj_type_info = { .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR (PwmOut) };
/**
* PwmOut#write (native JavaScript method)
@@ -47,25 +46,25 @@ static const jerry_object_native_info_t native_obj_type_info = {
* Values outside this range will be saturated to 0.0f or 1.0f
* @returns undefined
*/
DECLARE_CLASS_FUNCTION(PwmOut, write) {
CHECK_ARGUMENT_COUNT(PwmOut, write, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, write, 0, number);
DECLARE_CLASS_FUNCTION (PwmOut, write)
{
CHECK_ARGUMENT_COUNT (PwmOut, write, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, write, 0, number);
// Extract native PwmOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native PwmOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native PwmOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
}
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
double arg0 = jerry_get_number_value(args[0]);
native_ptr->write(static_cast<float>(arg0));
double arg0 = jerry_value_as_number (args[0]);
native_ptr->write (static_cast<float> (arg0));
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -81,22 +80,22 @@ DECLARE_CLASS_FUNCTION(PwmOut, write) {
* @note
* This value may not match exactly the value set by a previous <write>.
*/
DECLARE_CLASS_FUNCTION(PwmOut, read) {
CHECK_ARGUMENT_COUNT(PwmOut, read, (args_count == 0));
DECLARE_CLASS_FUNCTION (PwmOut, read)
{
CHECK_ARGUMENT_COUNT (PwmOut, read, (args_count == 0));
// Extract native PwmOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native PwmOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native PwmOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
}
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
float result = native_ptr->read();
return jerry_create_number(result);
float result = native_ptr->read ();
return jerry_number (result);
}
/**
@@ -108,25 +107,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, read) {
* The resolution is currently in microseconds; periods smaller than this
* will be set to zero.
*/
DECLARE_CLASS_FUNCTION(PwmOut, period) {
CHECK_ARGUMENT_COUNT(PwmOut, period, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period, 0, number);
DECLARE_CLASS_FUNCTION (PwmOut, period)
{
CHECK_ARGUMENT_COUNT (PwmOut, period, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, period, 0, number);
// Extract native PwmOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native PwmOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native PwmOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
}
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
double arg0 = jerry_get_number_value(args[0]);
native_ptr->period(static_cast<float>(arg0));
double arg0 = jerry_value_as_number (args[0]);
native_ptr->period (static_cast<float> (arg0));
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -134,25 +133,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, period) {
*
* Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
*/
DECLARE_CLASS_FUNCTION(PwmOut, period_ms) {
CHECK_ARGUMENT_COUNT(PwmOut, period_ms, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period_ms, 0, number);
DECLARE_CLASS_FUNCTION (PwmOut, period_ms)
{
CHECK_ARGUMENT_COUNT (PwmOut, period_ms, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, period_ms, 0, number);
// Extract native PwmOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native PwmOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native PwmOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
}
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
double arg0 = jerry_get_number_value(args[0]);
native_ptr->period_ms(static_cast<int>(arg0));
double arg0 = jerry_value_as_number (args[0]);
native_ptr->period_ms (static_cast<int> (arg0));
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -160,25 +159,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, period_ms) {
*
* Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
*/
DECLARE_CLASS_FUNCTION(PwmOut, period_us) {
CHECK_ARGUMENT_COUNT(PwmOut, period_us, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period_us, 0, number);
DECLARE_CLASS_FUNCTION (PwmOut, period_us)
{
CHECK_ARGUMENT_COUNT (PwmOut, period_us, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, period_us, 0, number);
// Extract native PwmOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native PwmOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native PwmOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
}
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
double arg0 = jerry_get_number_value(args[0]);
native_ptr->period_us(static_cast<int>(arg0));
double arg0 = jerry_value_as_number (args[0]);
native_ptr->period_us (static_cast<int> (arg0));
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -186,25 +185,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, period_us) {
*
* Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
*/
DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth) {
CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth, 0, number);
DECLARE_CLASS_FUNCTION (PwmOut, pulsewidth)
{
CHECK_ARGUMENT_COUNT (PwmOut, pulsewidth, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, pulsewidth, 0, number);
// Extract native PwmOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native PwmOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native PwmOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
}
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
double arg0 = jerry_get_number_value(args[0]);
native_ptr->pulsewidth(static_cast<float>(arg0));
double arg0 = jerry_value_as_number (args[0]);
native_ptr->pulsewidth (static_cast<float> (arg0));
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -212,25 +211,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth) {
*
* Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
*/
DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_ms) {
CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth_ms, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth_ms, 0, number);
DECLARE_CLASS_FUNCTION (PwmOut, pulsewidth_ms)
{
CHECK_ARGUMENT_COUNT (PwmOut, pulsewidth_ms, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, pulsewidth_ms, 0, number);
// Extract native PwmOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native PwmOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native PwmOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
}
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
double arg0 = jerry_get_number_value(args[0]);
native_ptr->pulsewidth_ms(static_cast<int>(arg0));
double arg0 = jerry_value_as_number (args[0]);
native_ptr->pulsewidth_ms (static_cast<int> (arg0));
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -238,25 +237,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_ms) {
*
* Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
*/
DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_us) {
CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth_us, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth_us, 0, number);
DECLARE_CLASS_FUNCTION (PwmOut, pulsewidth_us)
{
CHECK_ARGUMENT_COUNT (PwmOut, pulsewidth_us, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, pulsewidth_us, 0, number);
// Extract native PwmOut pointer
void* void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Extract native PwmOut pointer
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native PwmOut pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
}
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
double arg0 = jerry_get_number_value(args[0]);
native_ptr->pulsewidth_us(static_cast<int>(arg0));
double arg0 = jerry_value_as_number (args[0]);
native_ptr->pulsewidth_us (static_cast<int> (arg0));
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -265,28 +264,29 @@ DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_us) {
* @param pin_name mbed pin to connect the PwmOut to.
* @returns a JavaScript object representing a PwmOut.
*/
DECLARE_CLASS_CONSTRUCTOR(PwmOut) {
CHECK_ARGUMENT_COUNT(PwmOut, __constructor, args_count == 1);
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, __constructor, 0, number);
DECLARE_CLASS_CONSTRUCTOR (PwmOut)
{
CHECK_ARGUMENT_COUNT (PwmOut, __constructor, args_count == 1);
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, __constructor, 0, number);
PinName pin_name = PinName(jerry_get_number_value(args[0]));
PinName pin_name = PinName (jerry_value_as_number (args[0]));
// Create the native object
PwmOut* native_ptr = new PwmOut(pin_name);
// Create the native object
PwmOut* native_ptr = new PwmOut (pin_name);
// create the jerryscript object
jerry_value_t js_object = jerry_create_object();
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
// create the jerryscript object
jerry_value_t js_object = jerry_object ();
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
// attach methods
ATTACH_CLASS_FUNCTION(js_object, PwmOut, write);
ATTACH_CLASS_FUNCTION(js_object, PwmOut, read);
ATTACH_CLASS_FUNCTION(js_object, PwmOut, period);
ATTACH_CLASS_FUNCTION(js_object, PwmOut, period_ms);
ATTACH_CLASS_FUNCTION(js_object, PwmOut, period_us);
ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth);
ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth_ms);
ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth_us);
// attach methods
ATTACH_CLASS_FUNCTION (js_object, PwmOut, write);
ATTACH_CLASS_FUNCTION (js_object, PwmOut, read);
ATTACH_CLASS_FUNCTION (js_object, PwmOut, period);
ATTACH_CLASS_FUNCTION (js_object, PwmOut, period_ms);
ATTACH_CLASS_FUNCTION (js_object, PwmOut, period_us);
ATTACH_CLASS_FUNCTION (js_object, PwmOut, pulsewidth);
ATTACH_CLASS_FUNCTION (js_object, PwmOut, pulsewidth_ms);
ATTACH_CLASS_FUNCTION (js_object, PwmOut, pulsewidth_us);
return js_object;
return js_object;
}
@@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "jerryscript-mbed-drivers/setInterval-js.h"
#include "jerryscript-mbed-event-loop/EventLoop.h"
/**
@@ -23,26 +24,33 @@
* @param function Function to call
* @param interval Time between function calls, in ms.
*/
DECLARE_GLOBAL_FUNCTION(setInterval) {
CHECK_ARGUMENT_COUNT(global, setInterval, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 0, function);
CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 1, number);
DECLARE_GLOBAL_FUNCTION (setInterval)
{
CHECK_ARGUMENT_COUNT (global, setInterval, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS (global, setInterval, 0, function);
CHECK_ARGUMENT_TYPE_ALWAYS (global, setInterval, 1, number);
int interval = int(jerry_get_number_value(args[1]));
int interval = int (jerry_value_as_number (args[1]));
int id = mbed::js::EventLoop::getInstance().getQueue().call_every(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
int id = mbed::js::EventLoop::getInstance ().getQueue ().call_every (interval,
jerry_call,
args[0],
jerry_null (),
(jerry_value_t*) NULL,
0);
jerry_value_t result = jerry_set_property_by_index(call_info_p->function, id, args[0]);
jerry_value_t result = jerry_object_set_index (call_info_p->function, id, args[0]);
if (jerry_value_is_error(result)) {
jerry_release_value(result);
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
if (jerry_value_is_exception (result))
{
jerry_value_free (result);
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
return jerry_create_error(JERRY_ERROR_TYPE, (const jerry_char_t *) "Failed to run setInterval");
}
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to run setInterval");
}
jerry_release_value(result);
return jerry_create_number(id);
jerry_value_free (result);
return jerry_number (id);
}
/**
@@ -52,22 +60,23 @@ DECLARE_GLOBAL_FUNCTION(setInterval) {
*
* @param id ID of the timeout event, returned by setInterval.
*/
DECLARE_GLOBAL_FUNCTION(clearInterval) {
CHECK_ARGUMENT_COUNT(global, clearInterval, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(global, clearInterval, 0, number);
DECLARE_GLOBAL_FUNCTION (clearInterval)
{
CHECK_ARGUMENT_COUNT (global, clearInterval, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (global, clearInterval, 0, number);
int id = int(jerry_get_number_value(args[0]));
int id = int (jerry_value_as_number (args[0]));
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
jerry_value_t global_obj = jerry_get_global_object();
jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)"setInterval");
jerry_value_t func_obj = jerry_get_property(global_obj, prop_name);
jerry_release_value(prop_name);
jerry_value_t global_obj = jerry_current_realm ();
jerry_value_t prop_name = jerry_string_sz ("setInterval");
jerry_value_t func_obj = jerry_object_get (global_obj, prop_name);
jerry_value_free (prop_name);
jerry_delete_property_by_index(func_obj, id);
jerry_release_value(func_obj);
jerry_release_value(global_obj);
jerry_object_delete_index (func_obj, id);
jerry_value_free (func_obj);
jerry_value_free (global_obj);
return jerry_create_undefined();
return jerry_undefined ();
}
@@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "jerryscript-mbed-drivers/setTimeout-js.h"
#include "jerryscript-mbed-event-loop/EventLoop.h"
/**
@@ -23,26 +24,33 @@
* @param function Function to call
* @param wait_time Time before function is called, in ms.
*/
DECLARE_GLOBAL_FUNCTION(setTimeout) {
CHECK_ARGUMENT_COUNT(global, setTimeout, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS(global, setTimeout, 0, function);
CHECK_ARGUMENT_TYPE_ALWAYS(global, setTimeout, 1, number);
DECLARE_GLOBAL_FUNCTION (setTimeout)
{
CHECK_ARGUMENT_COUNT (global, setTimeout, (args_count == 2));
CHECK_ARGUMENT_TYPE_ALWAYS (global, setTimeout, 0, function);
CHECK_ARGUMENT_TYPE_ALWAYS (global, setTimeout, 1, number);
int interval = int(jerry_get_number_value(args[1]));
int interval = int (jerry_value_as_number (args[1]));
int id = mbed::js::EventLoop::getInstance().getQueue().call_in(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
int id = mbed::js::EventLoop::getInstance ().getQueue ().call_in (interval,
jerry_call,
args[0],
jerry_null (),
(jerry_value_t*) NULL,
0);
jerry_value_t result = jerry_set_property_by_index(call_info_p->function, id, args[0]);
jerry_value_t result = jerry_object_set_index (call_info_p->function, id, args[0]);
if (jerry_value_is_error(result)) {
jerry_release_value(result);
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
if (jerry_value_is_exception (result))
{
jerry_value_free (result);
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
return jerry_create_error(JERRY_ERROR_TYPE, (const jerry_char_t *) "Failed to run setTimeout");
}
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to run setTimeout");
}
jerry_release_value(result);
return jerry_create_number(id);
jerry_value_free (result);
return jerry_number (id);
}
/**
@@ -52,22 +60,23 @@ DECLARE_GLOBAL_FUNCTION(setTimeout) {
*
* @param id ID of the timeout event, returned by setTimeout.
*/
DECLARE_GLOBAL_FUNCTION(clearTimeout) {
CHECK_ARGUMENT_COUNT(global, clearTimeout, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(global, clearTimeout, 0, number);
DECLARE_GLOBAL_FUNCTION (clearTimeout)
{
CHECK_ARGUMENT_COUNT (global, clearTimeout, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (global, clearTimeout, 0, number);
int id = int(jerry_get_number_value(args[0]));
int id = int (jerry_value_as_number (args[0]));
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
jerry_value_t global_obj = jerry_get_global_object();
jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)"setTimeout");
jerry_value_t func_obj = jerry_get_property(global_obj, prop_name);
jerry_release_value(prop_name);
jerry_value_t global_obj = jerry_current_realm ();
jerry_value_t prop_name = jerry_string_sz ("setTimeout");
jerry_value_t func_obj = jerry_object_get (global_obj, prop_name);
jerry_value_free (prop_name);
jerry_delete_property_by_index(func_obj, id);
jerry_release_value(func_obj);
jerry_release_value(global_obj);
jerry_object_delete_index (func_obj, id);
jerry_value_free (func_obj);
jerry_value_free (global_obj);
return jerry_create_undefined();
return jerry_undefined ();
}