iotjs_jval_t iotjs_jval_create_string(const iotjs_string_t* v) { iotjs_jval_t jval; IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_jval_t, &jval); const jerry_char_t* data = (const jerry_char_t*)(iotjs_string_data(v)); jerry_size_t size = iotjs_string_size(v); _this->value = jerry_create_string_sz(data, size); return jval; }
//--------------CURL Callbacks------------------ // Read callback is actually to write data to outgoing request size_t iotjs_https_curl_read_callback(void* contents, size_t size, size_t nmemb, void* userp) { iotjs_https_t* https_data = (iotjs_https_t*)userp; IOTJS_VALIDATED_STRUCT_METHOD(iotjs_https_t, https_data); // If stream wasnt made writable yet, make it so. if (!_this->is_stream_writable) { _this->is_stream_writable = true; iotjs_https_jcallback(https_data, IOTJS_MAGIC_STRING_ONWRITABLE, iotjs_jargs_get_empty(), false); } if (_this->data_to_read) { size_t real_size = size * nmemb; size_t chunk_size = iotjs_string_size(&(_this->read_chunk)); size_t left_to_copy_size = chunk_size - _this->cur_read_index; if (real_size < 1) return 0; // send some data if (_this->cur_read_index < chunk_size) { size_t num_to_copy = (left_to_copy_size < real_size) ? left_to_copy_size : real_size; const char* buf = iotjs_string_data(&(_this->read_chunk)); buf = &buf[_this->cur_read_index]; strncpy((char*)contents, buf, num_to_copy); _this->cur_read_index = _this->cur_read_index + num_to_copy; return num_to_copy; } // Finished sending one chunk of data _this->cur_read_index = 0; _this->data_to_read = false; iotjs_https_call_read_onwrite_async(https_data); } // If the data is sent, and stream hasn't ended, wait for more data if (!_this->stream_ended) { return CURL_READFUNC_PAUSE; } // All done, end the transfer return 0; }
// This function should be able to print utf8 encoded string // as utf8 is internal string representation in Jerryscript static jerry_value_t Print(const jerry_value_t* jargv, const jerry_length_t jargc, FILE* out_fd) { JS_CHECK_ARGS(1, string); iotjs_string_t msg = JS_GET_ARG(0, string); const char* str = iotjs_string_data(&msg); unsigned str_len = iotjs_string_size(&msg); unsigned idx = 0; if (iotjs_console_out) { int level = (out_fd == stdout) ? DBGLEV_INFO : DBGLEV_ERR; iotjs_console_out(level, "%s", str); } else { for (idx = 0; idx < str_len; idx++) { if (str[idx] != 0) { fprintf(out_fd, "%c", str[idx]); } else { fprintf(out_fd, "\\u0000"); } } } iotjs_string_destroy(&msg); return jerry_create_undefined(); }