Skip to content

Update the API changes in mbed targets #1175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions targets/mbed/Makefile.mbed
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

# use TAB-8

TYPE = release
TARGET_LIST = k64f stm32f4 stm32f429i nucleo
JERRYHEAP ?= 16

Expand All @@ -33,10 +32,9 @@ else ifeq ($(TARGET), nucleo)
TARGET_DIR ?= /media/$(USER)/NODE_F401RE
endif

UPPERC_TARGET = $(shell echo $(TARGET) | tr a-z A-Z)
INTERM = build/obj-mbed-$(TARGET)
OUTPUT = build/bin/$(TYPE).mbed$(TARGET)
COPYTARGET = targets/mbed/libjerry
BUILD_DIR ?= build/mbed
UPPERC_TARGET ?= $(shell echo $(TARGET) | tr a-z A-Z)
COPYTARGET ?= targets/mbed/libjerry

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These indentations are shifted wrongly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the observation.

else
$(error This board ($(board)) is not supported!)
Expand All @@ -53,25 +51,26 @@ EXT_PORT_DIR := ""
all: jerry js2c yotta

jerry:
mkdir -p $(INTERM)
mkdir -p $(OUTPUT)
mkdir -p $(COPYTARGET)
cmake -B$(INTERM) -H./ \
mkdir -p $(BUILD_DIR)
cmake -B$(BUILD_DIR) -H./ \
-DENABLE_LTO=OFF \
-DENABLE_VALGRIND=OFF \
-DCMAKE_TOOLCHAIN_FILE=build/configs/toolchain_external.cmake \
-DFEATURE_VALGRIND=OFF \
-DCMAKE_TOOLCHAIN_FILE=cmake/toolchain_external.cmake \
-DJERRY_LIBC=OFF \
-DJERRY_CMDLINE=OFF \
-DCOMPILER_DEFAULT_LIBC=ON \
-DEXTERNAL_CMAKE_SYSTEM_PROCESSOR=arm7-m \
-DENABLE_ALL_IN_ONE=OFF \
-DEXTERNAL_CMAKE_SYSTEM_PROCESSOR=armv7l-hf \
-DEXTERNAL_CMAKE_C_COMPILER=arm-none-eabi-gcc \
-DEXTERNAL_CMAKE_C_COMPILER_ID=GNU \
-DEXTERNAL_COMPILE_FLAGS="$(EXT_CFLAGS)" \
-DEXTERNAL_MEM_HEAP_SIZE_KB=$(JERRYHEAP) \
-DEXTERNAL_PORT_DIR=$(EXT_PORT_DIR)
-DMEM_HEAP_SIZE_KB=$(JERRYHEAP) \

make -C $(INTERM) $(TYPE).external
cp `cat $(INTERM)/$(TYPE).external/list` $(OUTPUT)/.
cp $(OUTPUT)/lib$(TYPE).jerry-core.a $(COPYTARGET)/libjerrycore.a
cp $(OUTPUT)/lib$(TYPE).jerry-libm.lib.a $(COPYTARGET)/libjerrylibm.a
make -C$(BUILD_DIR) jerry-core
make -C$(BUILD_DIR) jerry-libm
cp $(BUILD_DIR)/lib/libjerry-core.a $(COPYTARGET)/libjerrycore.a
cp $(BUILD_DIR)/lib/libjerry-libm.a $(COPYTARGET)/libjerrylibm.a

js2c:
cd targets/mbed; ../tools/js2c.py;
Expand All @@ -83,7 +82,7 @@ yotta:

flash:
ifndef TARGET_DIR
st-flash write targets/mbed/build/$(YOTTA_TARGET)/source/jerry.bin 0x08000000
st-flash write targets/mbed/build/$(YOTTA_TARGET)/source/jerry.bin 0x08000000
else
@if [ ! -d "${TARGET_DIR}" ] ; then \
echo "The board not mounted at ${TARGET_DIR}"; \
Expand All @@ -95,6 +94,6 @@ flash:
@echo "Wait till LED flashing stops..."

clean:
rm -rf $(INTERM)
rm -rf $(COPYTARGET)
rm -rf $(OUTPUT)
rm -rf targets/mbed/build
105 changes: 62 additions & 43 deletions targets/mbed/source/jerry_extapi.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
* Copyright 2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,9 +17,9 @@
#include <stdlib.h>
#include <stdio.h>

#include "jerry-core/jerry.h"
#include "jerry-core/jerry-api.h"
#include "jerry_extapi.h"

#include "native_mbed.h"

#ifndef MIN
Expand All @@ -30,12 +31,11 @@
#define __UNSED__ __attribute__((unused))

#define DECLARE_HANDLER(NAME) \
static bool \
NAME ## _handler (const jerry_api_object_t * function_obj_p __UNSED__, \
const jerry_api_value_t * this_p __UNSED__, \
jerry_api_value_t * ret_val_p __UNSED__, \
const jerry_api_value_t args_p[], \
const jerry_api_length_t args_cnt)
static jerry_value_t \
NAME ## _handler (const jerry_value_t func_value __UNSED__, \
const jerry_value_t this_value __UNSED__, \
const jerry_value_t args[], \
const jerry_length_t args_cnt )

#define REGISTER_HANDLER(NAME) \
register_native_function ( # NAME, NAME ## _handler)
Expand All @@ -45,39 +45,50 @@ NAME ## _handler (const jerry_api_object_t * function_obj_p __UNSED__, \
DECLARE_HANDLER(assert)
{
if (args_cnt == 1
&& args_p[0].type == JERRY_API_DATA_TYPE_BOOLEAN
&& args_p[0].u.v_bool == true)
&& jerry_value_is_boolean (args[0])
&& jerry_get_boolean_value (args[0]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a double check, jerry_get_boolean_value will call this jerry_value_is_boolean function, and return with false if it isn't a boolean value.

Copy link
Contributor Author

@polaroi8d polaroi8d Jul 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its okey, jerry_value_is_boolean returns wheter the given argument is a boolean value and the jerry_get_boolean_value gets the raw value for it. Same like in the main-unix.c source file 80 in line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @polaroi8d. This would exploit a case, which was designed to avoid crashes when the API is misused. So this would promote a wrong API use.

{
printf (">> Jerry assert true\r\n");
return true;
return jerry_create_boolean (true);
}
printf ("ERROR: Script assertion failed\n");
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
return false;
return jerry_create_boolean (false);
}

DECLARE_HANDLER(led)
{
jerry_value_t ret_val;

if (args_cnt < 2)
{
return false;
ret_val = jerry_create_boolean (false);
printf ("Error: invalid arguments number!\r\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a return after this.

return ret_val;
}

if (!(jerry_value_is_number (args[0])
&& jerry_value_is_number (args[1])))
{
ret_val = jerry_create_boolean (false);
printf ("Error: arguments must be numbers!\r\n");
return ret_val;
}

int port, value;
port = (int)JS_VALUE_TO_NUMBER (&args_p[0]);
value = (int)JS_VALUE_TO_NUMBER (&args_p[1]);
port = (int) jerry_get_number_value (args[0]);
value = (int) jerry_get_number_value (args[1]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ensured that the arguments are numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, thanks for the hint.


ret_val_p->type = JERRY_API_DATA_TYPE_BOOLEAN;
if (port >=0 && port <= 3)
if (port >= 0 && port <= 3)
{
native_led(port, value);
ret_val_p->u.v_bool = true;
native_led (port, value);
ret_val = jerry_create_boolean (true);
}
else
{
ret_val_p->u.v_bool = false;
ret_val = jerry_create_boolean (false);
}
return true;
return ret_val;
}

//-----------------------------------------------------------------------------
Expand All @@ -86,41 +97,49 @@ static bool
register_native_function (const char* name,
jerry_external_handler_t handler)
{
jerry_api_object_t *global_obj_p;
jerry_api_object_t *reg_func_p;
jerry_api_value_t reg_value;
bool bok;
jerry_value_t global_object_val = jerry_get_global_object ();
jerry_value_t reg_function = jerry_create_external_function (handler);

global_obj_p = jerry_api_get_global ();
reg_func_p = jerry_api_create_external_function (handler);
bool is_ok = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is the same here, you continue the execution while is_ok is false. Why do you remove this return? The general rule is cleaning up and return when an error occures.


if (!(reg_func_p != NULL
&& jerry_api_is_function (reg_func_p)
&& jerry_api_is_constructor (reg_func_p)))
if (!(jerry_value_is_function (reg_function)
&& jerry_value_is_constructor (reg_function)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error flag should be checked. The returned value can be an object, which can also be a constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey.

{
is_ok = false;
printf ("Error: create_external_function failed !!!\r\n");
jerry_api_release_object (global_obj_p);
return false;
jerry_release_value (global_object_val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reg_function should be released too

jerry_release_value (reg_function);
return is_ok;
}

jerry_api_acquire_object (reg_func_p);
reg_value.type = JERRY_API_DATA_TYPE_OBJECT;
reg_value.u.v_object = reg_func_p;
if (jerry_value_has_error_flag (reg_function))
{
is_ok = false;
printf ("Error: create_external_function has error flag! \n\r");
jerry_release_value (global_object_val);
jerry_release_value (reg_function);
return is_ok;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you release this value somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, should I?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, returned value must be freed with jerry_release_object, when it is no longer needed.

Copy link
Contributor Author

@polaroi8d polaroi8d Jul 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey, thanks. FYI: There is no longer jerry_release_object after the api update.

bok = jerry_api_set_object_field_value (global_obj_p,
(jerry_api_char_t *) name,
&reg_value);
jerry_value_t jerry_name = jerry_create_string ((jerry_char_t *) name);

jerry_api_release_value (&reg_value);
jerry_api_release_object (reg_func_p);
jerry_api_release_object (global_obj_p);
jerry_value_t set_result = jerry_set_property (global_object_val,
jerry_name,
reg_function);

if (!bok)

if (jerry_value_has_error_flag (set_result))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

{
is_ok = false;
printf ("Error: register_native_function failed: [%s]\r\n", name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false should be returned if failed.

Copy link
Contributor

@LaszloLango LaszloLango Aug 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_result should be released in this case too. You should do something like this:

bool is_ok = true;

if (jerry_value_has_error_flag (set_result))
{
  is_ok = false;
  printf ("Error: register_native_function failed: [%s]\r\n", name);
}

jerry_release_value (set_result);
return is_ok;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returned with false if failed, added and released the set_result value

}

return bok;
jerry_release_value (jerry_name);
jerry_release_value (global_object_val);
jerry_release_value (reg_function);
jerry_release_value (set_result);

return is_ok;
}

void js_register_functions (void)
Expand Down
14 changes: 0 additions & 14 deletions targets/mbed/source/jerry_extapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@
#define JERRY_STANDALONE_EXIT_CODE_OK (0)
#define JERRY_STANDALONE_EXIT_CODE_FAIL (1)

#define API_DATA_IS_OBJECT(val_p) \
((val_p)->type == JERRY_API_DATA_TYPE_OBJECT)

#define API_DATA_IS_FUNCTION(val_p) \
(API_DATA_IS_OBJECT (val_p) && \
jerry_api_is_function ((val_p)->u.v_object))

#define JS_VALUE_TO_NUMBER(val_p) \
((val_p)->type == JERRY_API_DATA_TYPE_FLOAT32 ? \
static_cast<double>((val_p)->u.v_float32) : \
(val_p)->type == JERRY_API_DATA_TYPE_FLOAT64 ? \
static_cast<double>((val_p)->u.v_float64) : \
static_cast<double>((val_p)->u.v_uint32))

void js_register_functions (void);

#endif
Loading