Skip to content

String handling improvements #331

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
Apr 5, 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
4 changes: 2 additions & 2 deletions src/iotjs.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
59 changes: 4 additions & 55 deletions src/iotjs_binding.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<const jerry_api_char_t*>(v.data()));
_obj_val.u.v_string = jerry_api_create_string_sz(
reinterpret_cast<const jerry_api_char_t*>(v.data()), v.size());
_unref_at_close = true;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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<jerry_api_char_t*>(res.data());

Expand Down
20 changes: 1 addition & 19 deletions src/iotjs_binding.h
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/iotjs_module_buffer.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/iotjs_module_httpparser.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}


Expand Down Expand Up @@ -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);
}

Expand Down
34 changes: 13 additions & 21 deletions src/iotjs_module_process.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
Expand All @@ -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;
}

Expand All @@ -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()) {
Expand Down
16 changes: 5 additions & 11 deletions src/iotjs_util.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 4 additions & 1 deletion src/iotjs_util.h
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO this should be private if it is only for RVO.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have to fix myself. That would break RVO.


// Destructor
Expand Down