No whitespaces in string, what have I done wrong?

49 views
Skip to first unread message

idleman

unread,
May 1, 2012, 8:47:52 PM5/1/12
to v8-u...@googlegroups.com
Why does all white spaces get removed in the example below?

//C++
Handle<Value> System::log(const Arguments& args)
{
    for(int i = 0, s = args.Length(); i < s; ++i) {
       String::AsciiValue ascii(args[i]);
        os.write(*ascii, ascii.length()) << std::endl; //os = std::ostream& object
    }
    return Undefined();
}

//Then in javascript, there the System object have been "exposed" to JavaScript.
System.log("How are you?");  //prints "howareyou?" not "how are you?", e.g no whitespaces

In the example do I get the text: "Howareyou?", not "How are you?", what does I wrong? Why are all white spaces removed? The std::ostream::write write oformatted output, so it does not remove any white space, so it is not the problem.

Thanks in advance!

mdcb808

unread,
May 1, 2012, 9:22:22 PM5/1/12
to idleman, v8-u...@googlegroups.com

what happens if you replace your "// os = std::ostream& object" with
say, std::cerr? same code snippet for me, doesn't strip any white space:

for(int i = 0, s = args.Length(); i < s; ++i) {
v8::String::AsciiValue ascii(args[i]);
std::cerr.write(*ascii, ascii.length()) << std::endl;
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users

Stephan Beal

unread,
May 2, 2012, 3:06:16 AM5/2/12
to v8-u...@googlegroups.com

Google for std::iosteam skipws.

----- stephan beal
http://wanderinghorse.net/home/stephan/
http://gplus.to/sgbeal

idleman

unread,
May 2, 2012, 5:42:56 AM5/2/12
to v8-u...@googlegroups.com
Well, like a said before, the std::ostream object is not the problem, if i change the method to:

v8::Handle<v8::Value> System::log(const v8::Arguments& args)

{
    for(int i = 0, s = args.Length(); i < s; ++i) {
        v8::String::AsciiValue ascii(args[i]);
        std::cerr << "Length: " << ascii.length() << std::endl;
        std::cerr.write(*ascii,ascii.length()) << std::endl;
    }
    return v8::Undefined();
}

Will it print out the length without any whitespaces, e.g javascript code: System.log("      "); prints "Length: 0". However, do anyone have any idea what I can have done wrong? I am adding the method by doing something like the following, if it helps:

Handlescope handle_scope;
Persistent<Context> context = context(Context::New());
Context::Scope context_scope(context);
Local<ObjectTemplate> env = ObjectTemplate::New();
env->Set(String::New("log"), FunctionTemplate::New(&System::log)); //System.log is a static method of course
context->Global()->Set(String::New("System"), env->NewInstance());

Jakob Kummerow

unread,
May 2, 2012, 6:00:49 AM5/2/12
to v8-u...@googlegroups.com
I haven't checked where that logic lives, but I guess the string you're passing is automatically split up into several arguments along whitespace boundaries. Try printing the value of args.Length() to verify this. 
If things behave like command shells, you should be able to do System.log("\"multi word argument\""); to get a single argument consisting of several words.

Stephan Beal

unread,
May 2, 2012, 9:09:21 AM5/2/12
to v8-u...@googlegroups.com

The problem is formatted vs non-formatted output.  Write()  does unformatted. Stream operators use formatted i/o by default.

idleman

unread,
May 2, 2012, 12:53:51 PM5/2/12
to v8-u...@googlegroups.com
Now was it embarrassing, but I forgot that std::istream_iterator skip white spaces by default, which I used to read the JavaScript file, so no white spaces was not even compiled, but I assume it can happen all, thanks anyway!

Cheers
Reply all
Reply to author
Forward
0 new messages