Skip to content

Commit b3220ef

Browse files
committed
0.2.1-r57: added warn(); better err msg
1 parent 581ad26 commit b3220ef

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

k8.cc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define K8_VERSION "0.2.0-r53" // known to work with V8-3.16.3
1+
#define K8_VERSION "0.2.1-r57" // known to work with V8-3.16.14
22

33
#include <stdlib.h>
44
#include <stdint.h>
@@ -33,7 +33,7 @@
3333
*** Fundamental V8 routines ***
3434
*******************************/
3535

36-
const char *k8_cstr(const v8::String::AsciiValue &str) // Convert a V8 string to C string
36+
inline const char *k8_cstr(const v8::String::AsciiValue &str) // Convert a V8 string to C string
3737
{
3838
return *str? *str : "<N/A>";
3939
}
@@ -75,6 +75,7 @@ bool k8_execute(v8::Handle<v8::String> source, v8::Handle<v8::Value> name, bool
7575
{
7676
v8::HandleScope handle_scope;
7777
v8::TryCatch try_catch;
78+
if (source == v8::Handle<v8::String>()) return false;
7879
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
7980
if (script.IsEmpty()) {
8081
k8_exception(&try_catch);
@@ -99,7 +100,10 @@ bool k8_execute(v8::Handle<v8::String> source, v8::Handle<v8::Value> name, bool
99100
v8::Handle<v8::String> k8_readfile(const char *name) // Read the entire file. Copied from v8/shell.cc
100101
{
101102
FILE* file = fopen(name, "rb");
102-
if (file == NULL) return v8::Handle<v8::String>();
103+
if (file == NULL) {
104+
fprintf(stderr, "ERROR: fail to open file '%s'.\n", name);
105+
return v8::Handle<v8::String>();
106+
}
103107

104108
fseek(file, 0, SEEK_END);
105109
int size = ftell(file);
@@ -133,6 +137,18 @@ JS_METHOD(k8_print, args) // print(): print to stdout; TAB demilited if multiple
133137
return v8::Undefined();
134138
}
135139

140+
JS_METHOD(k8_warn, args) // print(): print to stdout; TAB demilited if multiple arguments are provided
141+
{
142+
for (int i = 0; i < args.Length(); i++) {
143+
v8::HandleScope handle_scope;
144+
if (i) fputc('\t', stderr);
145+
v8::String::AsciiValue str(args[i]);
146+
fputs(k8_cstr(str), stderr);
147+
}
148+
fputc('\n', stderr);
149+
return v8::Undefined();
150+
}
151+
136152
JS_METHOD(k8_exit, args) // exit()
137153
{
138154
int exit_code = args[0]->Int32Value();
@@ -636,6 +652,7 @@ static v8::Persistent<v8::Context> CreateShellContext() // adapted from shell.cc
636652
{
637653
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
638654
global->Set(JS_STR("print"), v8::FunctionTemplate::New(k8_print));
655+
global->Set(JS_STR("warn"), v8::FunctionTemplate::New(k8_warn));
639656
global->Set(JS_STR("exit"), v8::FunctionTemplate::New(k8_exit));
640657
global->Set(JS_STR("load"), v8::FunctionTemplate::New(k8_load));
641658
{ // add the 'Bytes' object

0 commit comments

Comments
 (0)