source: gaycko/trunk/src/js/glue.cpp @ 4768

Last change on this file since 4768 was 4768, checked in by Jean-Yves Lamoureux, 10 years ago
  • Reworked table handling
File size: 1.7 KB
Line 
1/*
2 *  Gaycko        Text mode web browser
3 *  Copyright (c) 2011 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12#include <string.h>
13#include <v8.h>
14#include "js/glue.h"
15
16using namespace v8;
17
18int x = 42;
19
20
21Handle<Value> XGetter(Local<String> property,
22                      const AccessorInfo& info) {
23    return Integer::New(x);
24}
25
26void XSetter(Local<String> property, Local<Value> value,
27             const AccessorInfo& info) {
28    x = value->Int32Value();
29}
30
31char* testV8(void) {
32        // Create a stack-allocated handle scope.
33        HandleScope handle_scope;
34   
35        // Create a new context.
36
37    Handle<ObjectTemplate> global_templ = ObjectTemplate::New();
38    global_templ->SetAccessor(String::New("x"), XGetter, XSetter);
39    Persistent<Context> context = Context::New(NULL, global_templ);
40   
41   
42   
43        // Enter the created context for compiling and
44        // running the hello world script.
45        Context::Scope context_scope(context);
46   
47        // Create a string containing the JavaScript source code.
48        Handle<String> source = String::New("'Hello' + ', World!' + x");
49   
50        // Compile the source code.
51        Handle<Script> script = Script::Compile(source);
52   
53        // Run the script to get the result.
54        Handle<Value> result = script->Run();
55   
56        // Dispose the persistent context.
57        context.Dispose();
58   
59        // Convert the result to an ASCII string and print it.
60        String::AsciiValue ascii(result);
61   
62       
63        return strdup(*ascii);
64}
Note: See TracBrowser for help on using the repository browser.