-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvsx_sample_module.h
145 lines (112 loc) · 3.65 KB
/
vsx_sample_module.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @author Dinesh Manajipet, Vovoid Media Technologies - Copyright (C) 2012-2020
* @see The GNU Public License (GPL)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef VSX_SAMPLE_MODULE_H
#define VSX_SAMPLE_MODULE_H
#include <v8.h>
#include "vsx_math_3d.h"
#include "vsx_param.h"
#include "vsx_module.h"
#include "vsx_timer.h"
using namespace v8;
class vsx_sample_module : public vsx_module
{
vsx_module_param_float* param1;
vsx_module_param_float* param2;
vsx_module_param_float* result;
vsx_timer tt;
HandleScope handle_scope;
Handle<Script> script;
Handle<String> source;
//Persistent<Context> context;
Handle<Context> context;
Handle<Value> v8result;
public:
void module_info(vsx_module_info* info)
{
info->identifier = "sample;module";
info->description = "Adds two input floats. And Returns an output float.";
info->in_param_spec ="param1:float,param2:float";;
info->out_param_spec = "result:float";
info->component_class = "parameters";
}
void declare_params(vsx_module_param_list& in_parameters, vsx_module_param_list& out_parameters)
{
param1 = (vsx_module_param_float*)in_parameters.create(VSX_MODULE_PARAM_ID_FLOAT,"param1");
param2 = (vsx_module_param_float*)in_parameters.create(VSX_MODULE_PARAM_ID_FLOAT,"param2");
result = (vsx_module_param_float*)out_parameters.create(VSX_MODULE_PARAM_ID_FLOAT,"result");
param1->set(0);
param2->set(0);
result->set(0);
loading_done = true;
}
bool init()
{
// Create a new context.
context = Context::New();
// Enter the created context for compiling and
// running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
source = String::New(
""
"foo = 1.0; "
"function test_function()"
"{ "
" foo+= 0.1;"
" return foo;"
"}"
);
/*
" var match = 0;"
" if(arguments[0] == arguments[1])"
" {"
" match = 1;"
" }"
*/
// Compile the source code.
script = Script::Compile(source);
// Run the script to get the result.
v8result = script->Run();
//context.Dispose();
return true;
}
void run()
{
tt.start();
Context::Scope context_scope(context);
Handle<Object> global = context->Global();
Handle<Value> value = global->Get(String::New("test_function"));
Handle<Function> func = Handle<Function>::Cast(value);
Handle<Value> args[2];
Handle<Value> js_result;
int final_result;
args[0] = String::New("1");
args[1] = String::New("1");
js_result = func->Call(global, 2, args);
String::AsciiValue ascii(js_result);
Number* js_numresult = Number::Cast( *js_result );
final_result = atoi(*ascii);
//context.Dispose();
float t = tt.dtime();
printf("%d result %d run in %f seconds\n", this, final_result, t);
printf(" %f\n", js_numresult->Value());
//result->set(param1->get()+param2->get());
}
};
#endif // VSX_SAMPLE_MODULE_H