Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 85e2d74

Browse files
Jimmy Huanggrgustaf
authored andcommitted
[general] Replace PRINT_FLOAT with new way of printing float (#1840)
Instead of using %f formating to print a floating point value, use JerryScript to convert the float to a string. Signed-off-by: Jimmy Huang <[email protected]>
1 parent 5d277d6 commit 85e2d74

File tree

6 files changed

+21
-46
lines changed

6 files changed

+21
-46
lines changed

Makefile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ endif # BOARD = arduino_101
168168

169169
# Print callback statistics during runtime
170170
CB_STATS ?= off
171-
# Print floats (uses -u _printf_float flag). This is a workaround on the A101
172-
# otherwise floats will not print correctly. It does use ~11k extra ROM though
173-
PRINT_FLOAT ?= off
174171

175172
ifeq ($(BOARD), linux)
176173
SNAPSHOT = off
@@ -285,10 +282,6 @@ analyze: $(JS)
285282
@if [ "$(SNAPSHOT)" = "on" ]; then \
286283
echo "add_definitions(-DZJS_SNAPSHOT_BUILD)" >> $(OUT)/$(BOARD)/generated.cmake; \
287284
fi
288-
@# Build NEWLIB with float print support, this will increase ROM size
289-
@if [ "$(PRINT_FLOAT)" = "on" ]; then \
290-
echo "CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y" >> prj.conf; \
291-
fi
292285
@# Add bluetooth debug configs if BLE is enabled
293286
@if grep -q BUILD_MODULE_BLE $(OUT)/$(BOARD)/generated.cmake; then \
294287
if [ "$(VARIANT)" = "debug" ]; then \
@@ -318,7 +311,6 @@ analyze: $(JS)
318311
-DJERRY_BASE=$(JERRY_BASE) \
319312
-DJERRY_OUTPUT=$(JERRY_OUTPUT) \
320313
-DJERRY_PROFILE=$(OUT)/$(BOARD)/jerry_feature.profile \
321-
-DPRINT_FLOAT=$(PRINT_FLOAT) \
322314
-DSNAPSHOT=$(SNAPSHOT) \
323315
-DVARIANT=$(VARIANT) \
324316
-DVERBOSITY=$(VERBOSITY) \

cmake/zjs.cmake

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ if("${CB_STATS}" STREQUAL "on")
4141
add_definitions(-DZJS_PRINT_CALLBACK_STATS)
4242
endif()
4343

44-
if("${PRINT_FLOAT}" STREQUAL "on")
45-
add_definitions(-DZJS_PRINT_FLOATS)
46-
endif()
47-
4844
if("${VARIANT}" STREQUAL "debug")
4945
add_definitions(-DDEBUG_BUILD -DOC_DEBUG)
5046
endif()

cmake/zjs_linux.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ add_definitions(
6969
-DZJS_LINUX_BUILD
7070
-DZJS_GPIO_MOCK
7171
-DZJS_FIND_FUNC_NAME
72-
-DZJS_PRINT_FLOATS
7372
)
7473

7574
set(APP_COMPILE_OPTIONS

scripts/trlite

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,11 @@ if [ "$RUN" == "all" -o "$RUN" == "1" ]; then
299299
MODULES=(aio ble dgram events gpio grove_lcd i2c performance pwm uart)
300300
SENSORS=(Accelerometer AmbientLightSensor Gyroscope Magnetometer TemperatureSensor)
301301
write_modules_test $TMPFILE $MODULES $SENSORS
302-
303-
# also, try printing a float w/o PRINT_FLOAT
304-
echo "console.log(3.14159);" >> $TMPFILE
305302
try_command "modules" make $VERBOSE JS=$TMPFILE ROM=255
306303

307304
MODULES=(net pme ws)
308305
write_modules_test $TMPFILE $MODULES
309-
310-
# also, try printing a float w/ PRINT_FLOAT
311-
echo "console.log(3.14159);" >> $TMPFILE
312-
313-
try_command "net" make $VERBOSE JS=$TMPFILE PRINT_FLOAT=on ROM=259
306+
try_command "net" make $VERBOSE JS=$TMPFILE ROM=255
314307

315308
# OCF test
316309
echo "var ocf = require('ocf');" > $TMPFILE
@@ -333,18 +326,11 @@ if [ "$RUN" == "all" -o "$RUN" == "2" ]; then
333326
MODULES=(ble board dgram events fs gpio grove_lcd i2c performance pwm uart)
334327
SENSORS=(Accelerometer)
335328
write_modules_test $TMPFILE $MODULES $SENSORS
336-
337-
# also, try printing a float w/o PRINT_FLOAT
338-
echo "console.log(3.14159);" >> $TMPFILE
339329
try_command "k64f module" make $VERBOSE JS=$TMPFILE ROM=256 BOARD=frdm_k64f
340330

341331
MODULES=(net ws)
342332
write_modules_test $TMPFILE $MODULES $SENSORS
343-
344-
# also, try printing a float w/ PRINT_FLOAT
345-
echo "console.log(3.14159);" >> $TMPFILE
346-
347-
try_command "k64f net" make $VERBOSE JS=$TMPFILE PRINT_FLOAT=on ROM=256 BOARD=frdm_k64f
333+
try_command "k64f net" make $VERBOSE JS=$TMPFILE ROM=256 BOARD=frdm_k64f
348334

349335
# OCF test
350336
echo "var ocf = require('ocf');" > $TMPFILE

src/zjs_console.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016-2017, Intel Corporation.
1+
// Copyright (c) 2016-2018, Intel Corporation.
22

33
#ifdef BUILD_MODULE_CONSOLE
44

@@ -54,6 +54,9 @@ static bool value2str(const jerry_value_t value, char *buf, int maxlen,
5454
// is true
5555
// returns: true if the representation was complete or false if it
5656
// was abbreviated
57+
ZVAL_MUTABLE str_val;
58+
bool is_string = false;
59+
5760
if (jerry_value_is_array(value)) {
5861
unsigned int len = jerry_get_array_length(value);
5962
sprintf(buf, "[Array - length %u]", len);
@@ -66,13 +69,8 @@ static bool value2str(const jerry_value_t value, char *buf, int maxlen,
6669
} else if (jerry_value_is_number(value)) {
6770
int type = is_int(value);
6871
if (type == IS_NUMBER) {
69-
#ifdef ZJS_PRINT_FLOATS
70-
double num = jerry_get_number_value(value);
71-
sprintf(buf, "%f", num);
72-
#else
73-
int num = (int)jerry_get_number_value(value);
74-
sprintf(buf, "[Float ~%d]", num);
75-
#endif
72+
str_val = jerry_value_to_string(value);
73+
is_string = true;
7674
} else if (type == IS_UINT) {
7775
unsigned int num = jerry_get_number_value(value);
7876
sprintf(buf, "%u", num);
@@ -87,23 +85,28 @@ static bool value2str(const jerry_value_t value, char *buf, int maxlen,
8785
else if (jerry_value_is_object(value)) {
8886
sprintf(buf, "[Object]");
8987
} else if (jerry_value_is_string(value)) {
90-
jerry_size_t size = jerry_get_string_size(value);
88+
str_val = jerry_value_to_string(value);
89+
is_string = true;
90+
} else if (jerry_value_is_undefined(value)) {
91+
sprintf(buf, "undefined");
92+
} else {
93+
// should never get this
94+
sprintf(buf, "UNKNOWN");
95+
}
96+
97+
if (is_string) {
98+
jerry_size_t size = jerry_get_string_size(str_val);
9199
if (size >= maxlen) {
92100
sprintf(buf, "[String - length %u]", (unsigned int)size);
93101
} else {
94102
char buffer[++size];
95-
zjs_copy_jstring(value, buffer, &size);
103+
zjs_copy_jstring(str_val, buffer, &size);
96104
if (quotes) {
97105
sprintf(buf, "\"%s\"", buffer);
98106
} else {
99107
sprintf(buf, "%s", buffer);
100108
}
101109
}
102-
} else if (jerry_value_is_undefined(value)) {
103-
sprintf(buf, "undefined");
104-
} else {
105-
// should never get this
106-
sprintf(buf, "UNKNOWN");
107110
}
108111
return true;
109112
}

tools/Makefile.snapshot

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ ifeq ($(shell uname -s), Linux)
4444
SNAPSHOT_LIBS += -pthread
4545
endif
4646

47-
SNAPSHOT_DEFINES += -DZJS_LINUX_BUILD \
48-
-DZJS_PRINT_FLOATS
47+
SNAPSHOT_DEFINES += -DZJS_LINUX_BUILD
4948

5049
SNAPSHOT_FLAGS += -fno-asynchronous-unwind-tables \
5150
-fno-omit-frame-pointer \

0 commit comments

Comments
 (0)