Skip to content

Commit a5e84a0

Browse files
committed
more refactoring, start completing Console API
1 parent 90e8154 commit a5e84a0

File tree

10 files changed

+570
-561
lines changed

10 files changed

+570
-561
lines changed

NativeScript/napi/common/native_api_util.h

Lines changed: 407 additions & 371 deletions
Large diffs are not rendered by default.

NativeScript/runtime/Console.cpp

Lines changed: 115 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,86 @@
11
#include "Console.h"
22

3-
#include <iostream>
3+
#include <sstream>
44
#include <string>
55

66
#include "js_native_api.h"
7+
#include "native_api_util.h"
8+
#include "runtime/RuntimeConfig.h"
9+
10+
#ifdef __APPLE__
11+
#include <CoreFoundation/CFString.h>
12+
void NSLog(CFStringRef format, ...);
13+
#else
14+
#include <iostream>
15+
#endif
716

817
namespace nativescript {
918

10-
void Console::Init(napi_env env) {
11-
napi_value global, Console, console;
12-
13-
napi_get_global(env, &global);
19+
JS_CLASS_INIT(Console::Init) {
20+
napi_value Console, console;
1421

1522
const napi_property_descriptor properties[] = {
16-
{
17-
.utf8name = "log",
18-
.name = nullptr,
19-
.method = Log,
20-
.getter = nullptr,
21-
.setter = nullptr,
22-
.value = nullptr,
23-
.attributes = napi_default,
24-
.data = (void*)kConsoleStreamLog,
25-
},
26-
{
27-
.utf8name = "error",
28-
.name = nullptr,
29-
.method = Log,
30-
.getter = nullptr,
31-
.setter = nullptr,
32-
.value = nullptr,
33-
.attributes = napi_default,
34-
.data = (void*)kConsoleStreamError,
35-
},
36-
{
37-
.utf8name = "warn",
38-
.name = nullptr,
39-
.method = Log,
40-
.getter = nullptr,
41-
.setter = nullptr,
42-
.value = nullptr,
43-
.attributes = napi_default,
44-
.data = (void*)kConsoleStreamWarn,
45-
},
23+
napi_util::desc("log", Log, (void*)kConsoleLogTypeLog),
24+
napi_util::desc("error", Log, (void*)kConsoleLogTypeError),
25+
napi_util::desc("warn", Log, (void*)kConsoleLogTypeWarn),
26+
napi_util::desc("info", Log, (void*)kConsoleLogTypeInfo),
27+
napi_util::desc("assert", Log, (void*)kConsoleLogTypeAssert),
28+
napi_util::desc("time", Time, nullptr),
29+
napi_util::desc("timeEnd", TimeEnd, nullptr),
30+
napi_util::desc("dir", Dir, nullptr),
31+
napi_util::desc("trace", Trace, nullptr),
4632
};
4733

4834
napi_define_class(env, "Console", NAPI_AUTO_LENGTH, Console::Constructor,
49-
nullptr, 3, properties, &Console);
35+
nullptr, 9, properties, &Console);
5036

5137
napi_new_instance(env, Console, 0, nullptr, &console);
5238

5339
const napi_property_descriptor globalProperties[] = {
54-
{
55-
.utf8name = "console",
56-
.name = nullptr,
57-
.method = nullptr,
58-
.getter = nullptr,
59-
.setter = nullptr,
60-
.value = console,
61-
.attributes = napi_default,
62-
.data = nullptr,
63-
},
64-
{
65-
.utf8name = "Console",
66-
.name = nullptr,
67-
.method = nullptr,
68-
.getter = nullptr,
69-
.setter = nullptr,
70-
.value = Console,
71-
.attributes = napi_default,
72-
.data = nullptr,
73-
},
40+
napi_util::desc("Console", Console),
41+
napi_util::desc("console", console),
7442
};
7543

7644
napi_define_properties(env, global, 2, globalProperties);
7745
}
7846

79-
napi_value Console::Constructor(napi_env env, napi_callback_info cbinfo) {
47+
JS_METHOD(Console::Constructor) {
8048
napi_value thisArg;
8149
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
8250
return thisArg;
8351
}
8452

85-
napi_value Console::Log(napi_env env, napi_callback_info cbinfo) {
53+
JS_METHOD(Console::Log) {
54+
if (!RuntimeConfig.LogToSystemConsole) {
55+
return UNDEFINED;
56+
}
57+
8658
size_t argc = 0;
87-
ConsoleStream stream;
59+
ConsoleLogType stream;
8860
void* data = nullptr;
8961

9062
napi_get_cb_info(env, cbinfo, &argc, nullptr, nullptr, &data);
9163

92-
stream = ConsoleStream((unsigned long)data);
64+
stream = ConsoleLogType((unsigned long)data);
65+
66+
size_t initialArg = 0;
67+
68+
if (stream == kConsoleLogTypeAssert) {
69+
bool passes = false;
70+
71+
if (argc > 0) {
72+
napi_value firstArg = nullptr;
73+
napi_get_cb_info(env, cbinfo, &argc, &firstArg, nullptr, nullptr);
74+
napi_coerce_to_bool(env, firstArg, &firstArg);
75+
napi_get_value_bool(env, firstArg, &passes);
76+
}
77+
78+
if (!passes) {
79+
initialArg = 1;
80+
} else {
81+
return UNDEFINED;
82+
}
83+
}
9384

9485
napi_value argv[argc];
9586
napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, nullptr);
@@ -102,9 +93,31 @@ napi_value Console::Log(napi_env env, napi_callback_info cbinfo) {
10293
&symbolDescription);
10394
napi_call_function(env, global, SymbolFor, 1, &symbolDescription, &symbol);
10495

105-
std::string string;
96+
std::stringstream log;
97+
98+
// TODO(dj): what if we made this pretty?
10699

107-
for (size_t i = 0; i < argc; i++) {
100+
log << "CONSOLE";
101+
switch (stream) {
102+
case kConsoleLogTypeLog:
103+
log << " LOG";
104+
break;
105+
case kConsoleLogTypeError:
106+
log << " ERROR";
107+
break;
108+
case kConsoleLogTypeWarn:
109+
log << " WARN";
110+
break;
111+
case kConsoleLogTypeInfo:
112+
log << " INFO";
113+
break;
114+
case kConsoleLogTypeAssert:
115+
log << " ASSERT FAILED";
116+
break;
117+
}
118+
log << ": ";
119+
120+
for (size_t i = initialArg; i < argc; i++) {
108121
napi_valuetype type;
109122
napi_typeof(env, argv[i], &type);
110123

@@ -129,43 +142,55 @@ napi_value Console::Log(napi_env env, napi_callback_info cbinfo) {
129142
napi_get_value_string_utf8(env, argstr, strbuf, length + 2, &length);
130143
strbuf[length] = i >= (argc - 1) ? '\0' : ' ';
131144
strbuf[length + 1] = '\0';
132-
string += strbuf;
145+
log << strbuf;
146+
free(strbuf);
133147
}
134148

135-
// TODO(dj): what if we made this pretty?
149+
log << "\n";
136150

137-
std::string log;
138-
log += "[JS]";
139-
switch (stream) {
140-
case kConsoleStreamLog:
141-
// log += "LOG";
142-
break;
143-
case kConsoleStreamError:
144-
log += " ERROR";
145-
break;
146-
case kConsoleStreamWarn:
147-
log += " WARN";
148-
break;
149-
}
150-
log += " ";
151-
log += string;
152-
log += "\n";
151+
std::string logString = log.str();
153152

153+
#ifdef __APPLE__
154+
NSLog(CFSTR("%s"), logString.c_str());
155+
#else
154156
switch (stream) {
155-
case kConsoleStreamLog:
156-
std::cout << log;
157-
break;
158-
case kConsoleStreamError:
159-
std::cerr << log;
157+
case kConsoleLogTypeLog:
158+
case kConsoleLogTypeInfo:
159+
std::cout << logString;
160160
break;
161-
case kConsoleStreamWarn:
162-
std::cerr << log;
161+
case kConsoleLogTypeError:
162+
case kConsoleLogTypeWarn:
163+
case kConsoleLogTypeAssert:
164+
std::cerr << logString;
163165
break;
164166
}
167+
#endif
168+
169+
return UNDEFINED;
170+
}
171+
172+
JS_METHOD(Console::Time) {
173+
napi_value thisArg;
174+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
175+
return thisArg;
176+
}
165177

166-
napi_value undefined;
167-
napi_get_undefined(env, &undefined);
168-
return undefined;
178+
JS_METHOD(Console::TimeEnd) {
179+
napi_value thisArg;
180+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
181+
return thisArg;
182+
}
183+
184+
JS_METHOD(Console::Dir) {
185+
napi_value thisArg;
186+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
187+
return thisArg;
188+
}
189+
190+
JS_METHOD(Console::Trace) {
191+
napi_value thisArg;
192+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
193+
return thisArg;
169194
}
170195

171196
} // namespace nativescript

NativeScript/runtime/Console.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
#ifndef CONSOLE_H
22
#define CONSOLE_H
33

4-
#include "js_native_api_types.h"
4+
#include "native_api_util.h"
55

66
namespace nativescript {
77

8-
enum ConsoleStream {
9-
kConsoleStreamLog,
10-
kConsoleStreamError,
11-
kConsoleStreamWarn,
8+
enum ConsoleLogType {
9+
kConsoleLogTypeLog,
10+
kConsoleLogTypeError,
11+
kConsoleLogTypeWarn,
12+
kConsoleLogTypeInfo,
13+
kConsoleLogTypeAssert,
1214
};
1315

1416
class Console {
1517
public:
16-
static void Init(napi_env env);
18+
static JS_CLASS_INIT(Init);
1719

18-
static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
20+
static JS_METHOD(Constructor);
1921

20-
static napi_value Log(napi_env env, napi_callback_info cbinfo);
22+
static JS_METHOD(Log);
23+
24+
static JS_METHOD(Time);
25+
26+
static JS_METHOD(TimeEnd);
27+
28+
static JS_METHOD(Dir);
29+
30+
static JS_METHOD(Trace);
2131
};
2232

2333
} // namespace nativescript

NativeScript/runtime/NapiUtil.h

Lines changed: 0 additions & 17 deletions
This file was deleted.

NativeScript/runtime/Performance.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
#include "js_native_api.h"
44
#include "mach/mach_time.h"
5+
#include "native_api_util.h"
56

67
namespace nativescript {
78

8-
void Performance::Init(napi_env env) {
9-
napi_value global, Performance, performance;
10-
11-
napi_get_global(env, &global);
9+
JS_CLASS_INIT(Performance::Init) {
10+
napi_value Performance, performance;
1211

1312
const napi_property_descriptor properties[] = {
1413
{
@@ -45,14 +44,14 @@ void Performance::Init(napi_env env) {
4544
napi_define_properties(env, global, 1, globalProperties);
4645
}
4746

48-
napi_value Performance::Constructor(napi_env env, napi_callback_info cbinfo) {
47+
JS_METHOD(Performance::Constructor) {
4948
napi_value thisArg;
5049
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
5150

5251
return thisArg;
5352
}
5453

55-
napi_value Performance::Now(napi_env env, napi_callback_info cbinfo) {
54+
JS_METHOD(Performance::Now) {
5655
uint64_t time = mach_absolute_time();
5756
mach_timebase_info_data_t timebase;
5857
mach_timebase_info(&timebase);

NativeScript/runtime/Performance.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#ifndef PERFORMANCE_H
22
#define PERFORMANCE_H
33

4-
#include "js_native_api_types.h"
4+
#include "native_api_util.h"
55

66
namespace nativescript {
77

88
class Performance {
99
public:
10-
static void Init(napi_env env);
10+
static JS_CLASS_INIT(Init);
1111

12-
static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
12+
static JS_METHOD(Constructor);
1313

14-
static napi_value Now(napi_env env, napi_callback_info cbinfo);
14+
static JS_METHOD(Now);
1515
};
1616

1717
} // namespace nativescript

0 commit comments

Comments
 (0)