OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "execution.h" | 33 #include "execution.h" |
34 #include "factory.h" | 34 #include "factory.h" |
35 #include "platform.h" | 35 #include "platform.h" |
36 #include "top.h" | 36 #include "top.h" |
37 #include "cctest.h" | 37 #include "cctest.h" |
38 | 38 |
39 using namespace v8::internal; | 39 using namespace v8::internal; |
40 | 40 |
41 static v8::Persistent<v8::Context> env; | 41 static v8::Persistent<v8::Context> env; |
42 | 42 |
| 43 // --- P r i n t E x t e n s i o n --- |
| 44 |
| 45 class PrintExtension : public v8::Extension { |
| 46 public: |
| 47 PrintExtension() : v8::Extension("v8/print", kSource) { } |
| 48 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 49 v8::Handle<v8::String> name); |
| 50 static v8::Handle<v8::Value> Print(const v8::Arguments& args); |
| 51 private: |
| 52 static const char* kSource; |
| 53 }; |
| 54 |
| 55 |
| 56 const char* PrintExtension::kSource = "native function print();"; |
| 57 |
| 58 |
| 59 v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction( |
| 60 v8::Handle<v8::String> str) { |
| 61 return v8::FunctionTemplate::New(PrintExtension::Print); |
| 62 } |
| 63 |
| 64 |
| 65 v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) { |
| 66 for (int i = 0; i < args.Length(); i++) { |
| 67 if (i != 0) printf(" "); |
| 68 v8::HandleScope scope; |
| 69 v8::Handle<v8::Value> arg = args[i]; |
| 70 v8::Handle<v8::String> string_obj = arg->ToString(); |
| 71 if (string_obj.IsEmpty()) return string_obj; |
| 72 int length = string_obj->Length(); |
| 73 uint16_t* string = NewArray<uint16_t>(length + 1); |
| 74 string_obj->Write(string); |
| 75 for (int j = 0; j < length; j++) |
| 76 printf("%lc", string[j]); |
| 77 DeleteArray(string); |
| 78 } |
| 79 printf("\n"); |
| 80 return v8::Undefined(); |
| 81 } |
| 82 |
| 83 |
| 84 static PrintExtension kPrintExtension; |
| 85 v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension); |
| 86 |
| 87 |
43 static void InitializeVM() { | 88 static void InitializeVM() { |
44 if (env.IsEmpty()) { | 89 if (env.IsEmpty()) { |
45 v8::HandleScope scope; | 90 v8::HandleScope scope; |
46 const char* extensions[] = { "v8/print", "v8/gc" }; | 91 const char* extensions[] = { "v8/print", "v8/gc" }; |
47 v8::ExtensionConfiguration config(2, extensions); | 92 v8::ExtensionConfiguration config(2, extensions); |
48 env = v8::Context::New(&config); | 93 env = v8::Context::New(&config); |
49 } | 94 } |
50 v8::HandleScope scope; | 95 v8::HandleScope scope; |
51 env->Enter(); | 96 env->Enter(); |
52 } | 97 } |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 *Factory::LookupAsciiSymbol("foo"))); | 295 *Factory::LookupAsciiSymbol("foo"))); |
251 CHECK(fun1->IsJSFunction()); | 296 CHECK(fun1->IsJSFunction()); |
252 | 297 |
253 Object** argv[1] = { | 298 Object** argv[1] = { |
254 Handle<Object>::cast(Factory::LookupAsciiSymbol("hello")).location() | 299 Handle<Object>::cast(Factory::LookupAsciiSymbol("hello")).location() |
255 }; | 300 }; |
256 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv, | 301 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv, |
257 &has_pending_exception); | 302 &has_pending_exception); |
258 CHECK(!has_pending_exception); | 303 CHECK(!has_pending_exception); |
259 } | 304 } |
OLD | NEW |