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
RevLine 
[4748]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 */
[4727]12#include <string.h>
[4724]13#include <v8.h>
[4725]14#include "js/glue.h"
[4724]15
16using namespace v8;
17
[4768]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
[4727]31char* testV8(void) {
[4724]32        // Create a stack-allocated handle scope.
33        HandleScope handle_scope;
[4768]34   
[4724]35        // Create a new context.
36
[4768]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   
[4724]43        // Enter the created context for compiling and
44        // running the hello world script.
45        Context::Scope context_scope(context);
[4768]46   
[4724]47        // Create a string containing the JavaScript source code.
[4768]48        Handle<String> source = String::New("'Hello' + ', World!' + x");
49   
[4724]50        // Compile the source code.
51        Handle<Script> script = Script::Compile(source);
[4768]52   
[4724]53        // Run the script to get the result.
54        Handle<Value> result = script->Run();
[4768]55   
[4724]56        // Dispose the persistent context.
57        context.Dispose();
[4768]58   
[4724]59        // Convert the result to an ASCII string and print it.
60        String::AsciiValue ascii(result);
[4768]61   
62       
[4727]63        return strdup(*ascii);
[4724]64}
Note: See TracBrowser for help on using the repository browser.