diff --git a/src/iotjs.cpp b/src/iotjs.cpp index 6d8fc2e8f7..c7c6983548 100644 --- a/src/iotjs.cpp +++ b/src/iotjs.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. +/* Copyright 2015-2016 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,7 +91,7 @@ static void CleanupModules() { static bool RunIoTjs(JObject* process) { // Evaluating 'iotjs.js' returns a function. #ifndef ENABLE_SNAPSHOT - JResult jmain = JObject::Eval(String(iotjs_s), false, false); + JResult jmain = JObject::Eval(String(iotjs_s, iotjs_l), false, false); #else JResult jmain = JObject::ExecSnapshot(iotjs_s, iotjs_l); #endif diff --git a/src/iotjs_binding.cpp b/src/iotjs_binding.cpp index 5179e343f0..d63c152bac 100644 --- a/src/iotjs_binding.cpp +++ b/src/iotjs_binding.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. +/* Copyright 2015-2016 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -105,10 +105,9 @@ JObject::JObject(const char* v) { JObject::JObject(const String& v) { - IOTJS_ASSERT(!v.IsEmpty()); _obj_val.type = JERRY_API_DATA_TYPE_STRING; - _obj_val.u.v_string = jerry_api_create_string( - reinterpret_cast(v.data())); + _obj_val.u.v_string = jerry_api_create_string_sz( + reinterpret_cast(v.data()), v.size()); _unref_at_close = true; } @@ -170,71 +169,36 @@ JObject JObject::Error(const char* message) { } -JObject JObject::Error(const String& message) { - return CreateError(message.data(), JERRY_API_ERROR_COMMON); -} - - JObject JObject::EvalError(const char* message) { return CreateError(message, JERRY_API_ERROR_EVAL); } -JObject JObject::EvalError(const String& message) { - return CreateError(message.data(), JERRY_API_ERROR_EVAL); -} - - JObject JObject::RangeError(const char* message) { return CreateError(message, JERRY_API_ERROR_RANGE); } -JObject JObject::RangeError(const String& message) { - return CreateError(message.data(), JERRY_API_ERROR_RANGE); -} - - JObject JObject::ReferenceError(const char* message) { return CreateError(message, JERRY_API_ERROR_REFERENCE); } -JObject JObject::ReferenceError(const String& message) { - return CreateError(message.data(), JERRY_API_ERROR_REFERENCE); -} - - JObject JObject::SyntaxError(const char* message) { return CreateError(message, JERRY_API_ERROR_SYNTAX); } -JObject JObject::SyntaxError(const String& message) { - return CreateError(message.data(), JERRY_API_ERROR_SYNTAX); -} - - JObject JObject::TypeError(const char* message) { return CreateError(message, JERRY_API_ERROR_TYPE); } -JObject JObject::TypeError(const String& message) { - return CreateError(message.data(), JERRY_API_ERROR_TYPE); -} - - JObject JObject::URIError(const char* message) { return CreateError(message, JERRY_API_ERROR_URI); } -JObject JObject::URIError(const String& message) { - return CreateError(message.data(), JERRY_API_ERROR_URI); -} - - JResult JObject::Eval(const String& source, bool direct_mode, bool strict_mode) { @@ -275,11 +239,6 @@ void JObject::SetProperty(const char* name, const JObject& val) { } -void JObject::SetProperty(const String& name, const JObject& val) { - SetProperty(name.data(), val); -} - - void JObject::SetProperty(const char* name, JRawValueType val) { IOTJS_ASSERT(IsObject()); bool is_ok = jerry_api_set_object_field_value( @@ -290,11 +249,6 @@ void JObject::SetProperty(const char* name, JRawValueType val) { } -void JObject::SetProperty(const String& name, JRawValueType val) { - SetProperty(name.data(), val); -} - - JObject JObject::GetProperty(const char* name) { IOTJS_ASSERT(IsObject()); JRawValueType res; @@ -307,11 +261,6 @@ JObject JObject::GetProperty(const char* name) { } -JObject JObject::GetProperty(const String& name) { - return GetProperty(name.data()); -} - - void JObject::Ref() { if (JVAL_IS_STRING(&_obj_val)) { jerry_api_acquire_string(_obj_val.u.v_string); @@ -446,7 +395,7 @@ String JObject::GetString() { jerry_api_size_t size = jerry_api_get_string_size(_obj_val.u.v_string); - String res("", size); + String res(NULL, size); jerry_api_char_t* buffer = reinterpret_cast(res.data()); diff --git a/src/iotjs_binding.h b/src/iotjs_binding.h index 9f729294ab..58f745b87e 100644 --- a/src/iotjs_binding.h +++ b/src/iotjs_binding.h @@ -1,4 +1,4 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. +/* Copyright 2015-2016 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,26 +91,12 @@ class JObject { // Create a javascript error object. static JObject Error(const char* message); - static JObject Error(const String& message); - static JObject EvalError(const char* message); - static JObject EvalError(const String& message); - static JObject RangeError(const char* message); - static JObject RangeError(const String& message); - static JObject ReferenceError(const char* message); - static JObject ReferenceError(const String& message); - static JObject SyntaxError(const char* message); - static JObject SyntaxError(const String& message); - static JObject TypeError(const char* message); - static JObject TypeError(const String& message); - static JObject URIError(const char* message); - static JObject URIError(const String& message); - // Evaluate javascript source file. static JResult Eval(const String& source, @@ -143,13 +129,9 @@ class JObject { // Sets & gets property for the javascript object. void SetProperty(const char* name, const JObject& val); - void SetProperty(const String& name, const JObject& val); - void SetProperty(const char* name, JRawValueType val); - void SetProperty(const String& name, JRawValueType val); JObject GetProperty(const char* name); - JObject GetProperty(const String& name); // Sets & gets native data for the javascript object. void SetNative(uintptr_t ptr, JFreeHandlerType free_handler); diff --git a/src/iotjs_module_buffer.cpp b/src/iotjs_module_buffer.cpp index 4ac895281a..85ac0aec65 100644 --- a/src/iotjs_module_buffer.cpp +++ b/src/iotjs_module_buffer.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. +/* Copyright 2015-2016 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -268,9 +268,9 @@ JHANDLER_FUNCTION(ToString) { int length = end - start; JHANDLER_CHECK(length >= 0); - String str("", length + 1); + length = strnlen(buffer_wrap->buffer() + start, length); - strncpy(str.data(), buffer_wrap->buffer() + start, length); + String str(buffer_wrap->buffer() + start, length); JObject ret(str); handler.Return(ret); diff --git a/src/iotjs_module_httpparser.cpp b/src/iotjs_module_httpparser.cpp index d63ed0480c..4177dccf03 100644 --- a/src/iotjs_module_httpparser.cpp +++ b/src/iotjs_module_httpparser.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. +/* Copyright 2015-2016 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -116,8 +116,8 @@ class HTTPParserWrap : public JObjectWrap { char index_string1 [5]; sprintf(index_string0,"%d",i*2); sprintf(index_string1,"%d",i*2+1); - JObject v(values[i].data()); - JObject f(fields[i].data()); + JObject v(values[i]); + JObject f(fields[i]); jheader.SetProperty(index_string0, f); jheader.SetProperty(index_string1, v); @@ -147,7 +147,7 @@ class HTTPParserWrap : public JObjectWrap { JSETPROPERTY(info, "headers", makeHeader()); if ( parser.type == HTTP_REQUEST) { IOTJS_ASSERT(!url.IsEmpty()); - JSETPROPERTY(info, "url", url.data()); + JSETPROPERTY(info, "url", url); } } n_fields = n_values = 0; @@ -160,7 +160,7 @@ class HTTPParserWrap : public JObjectWrap { // Status if (parser.type == HTTP_RESPONSE) { JSETPROPERTY(info, "status", (int32_t)parser.status_code); - JSETPROPERTY(info, "status_msg", status_msg.data()); + JSETPROPERTY(info, "status_msg", status_msg); } @@ -218,7 +218,7 @@ class HTTPParserWrap : public JObjectWrap { JObject jheader(makeHeader()); argv.Add(jheader); if (parser.type == HTTP_REQUEST && !url.IsEmpty()) { - JObject jurl(url.data()); + JObject jurl(url); argv.Add(jurl); } diff --git a/src/iotjs_module_process.cpp b/src/iotjs_module_process.cpp index d51407aded..960a591ab4 100644 --- a/src/iotjs_module_process.cpp +++ b/src/iotjs_module_process.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. +/* Copyright 2015-2016 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -121,19 +121,18 @@ JHANDLER_FUNCTION(Binding) { } -static JResult WrapEval(const char* source) { +static JResult WrapEval(const char* source, size_t length) { static const char* wrapper[2] = { "(function(exports, require, module) {\n", "});\n" }; - int len1 = strlen(wrapper[0]); - int len2 = strlen(source); - int len3 = strlen(wrapper[1]); + int len0 = strlen(wrapper[0]); + int len1 = strlen(wrapper[1]); - String code(NULL, len1 + len2 + len3); + String code(NULL, len0 + length + len1); strcpy(code.data(), wrapper[0]); - strcpy(code.data() + len1, source); - strcpy(code.data() + len1 + len2, wrapper[1]); + strcpy(code.data() + len0, source); + strcpy(code.data() + len0 + length, wrapper[1]); return JObject::Eval(code); } @@ -143,7 +142,9 @@ JHANDLER_FUNCTION(Compile){ JHANDLER_CHECK(handler.GetArgLength() == 1); JHANDLER_CHECK(handler.GetArg(0)->IsString()); - JResult jres = WrapEval(handler.GetArg(0)->GetString().data()); + String source = handler.GetArg(0)->GetString(); + + JResult jres = WrapEval(source.data(), source.size()); if (jres.IsOk()) { handler.Return(jres.value()); @@ -159,20 +160,11 @@ JHANDLER_FUNCTION(CompileNativePtr){ JHANDLER_CHECK(handler.GetArgLength() == 1); JHANDLER_CHECK(handler.GetArg(0)->IsString()); - String id (handler.GetArg(0)->GetString()); + String id = handler.GetArg(0)->GetString(); int i=0; while (natives[i].name != NULL) { - const char *name_iter_p = natives[i].name; - size_t name_len = 0; - while (*name_iter_p != '\0') - { - name_len++; - name_iter_p++; - } - - if (name_len == (size_t) id.size () - && !strncmp (natives[i].name, id.data(), id.size())) { + if (!strcmp(natives[i].name, id.data())) { break; } @@ -184,7 +176,7 @@ JHANDLER_FUNCTION(CompileNativePtr){ JResult jres = JObject::ExecSnapshot(natives[i].code, natives[i].length); #else - JResult jres = WrapEval((const char*)natives[i].code); + JResult jres = WrapEval((const char*)natives[i].code, natives[i].length); #endif if (jres.IsOk()) { diff --git a/src/iotjs_util.cpp b/src/iotjs_util.cpp index e59215e25b..df003df016 100644 --- a/src/iotjs_util.cpp +++ b/src/iotjs_util.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. +/* Copyright 2015-2016 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,18 +33,12 @@ String ReadFile(const char* path) { IOTJS_ASSERT(len >= 0); fseek(file, 0, SEEK_SET); - String contents(NULL, 0, len); + String contents(NULL, len); - char buff[128]; - size_t total = 0; + size_t read = fread(contents.data(), 1, len, file); + IOTJS_ASSERT(read == len); - while (total < len) { - size_t read = fread(buff, 1, 128, file); - IOTJS_ASSERT(read > 0); - - contents.Append(buff, read); - total += read; - } + *(contents.data() + len) = 0; fclose(file); diff --git a/src/iotjs_util.h b/src/iotjs_util.h index 6dc91d2385..1ccd3ad622 100644 --- a/src/iotjs_util.h +++ b/src/iotjs_util.h @@ -1,4 +1,4 @@ -/* Copyright 2015 Samsung Electronics Co., Ltd. +/* Copyright 2015-2016 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,6 +51,9 @@ class String { explicit String(const char* data, int size = -1, int cap = -1); // Create string object from other string object. + // (Actually unimplemented. Declaration of copy constructor is needed for + // return value optimization to work. However, linker error will be emitted + // if copy construction is used in any other situation.) String(const String& other); // Destructor