2
# This file is part of GNU Enterprise.
4
# GNU Enterprise is free software; you can redistribute it
5
# and/or modify it under the terms of the GNU General Public
6
# License as published by the Free Software Foundation; either
7
# version 2, or (at your option) any later version.
9
# GNU Enterprise is distributed in the hope that it will be
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public
15
# License along with program; see the file COPYING. If not,
16
# write to the Free Software Foundation, Inc., 59 Temple Place
17
# - Suite 330, Boston, MA 02111-1307, USA.
19
# Copyright 2003-2005 Free Software Foundation
23
# ECMAscript/Adapter.py
26
# Provides a way to execute python code in a given environment
34
from gnue.common.apps import GDebug
35
from gnue.common.apps import errors
36
from gnue.common.logic import language
37
from gnue.common.logic.adapters import Base
38
from gnue.common.logic.NamespaceCore import NamespaceElement
41
from spidermonkey import Runtime
43
print 'Spidermonkey python module not installed!'
44
print 'You can get it from http://wwwsearch.sourceforge.net/python-spidermonkey/!'
47
class LanguageAdapter(Base.BaseLanguageAdapter):
51
def createNewContext(self):
52
return ExecutionContext(self._rt)
54
class ExecutionContext(Base.BaseExecutionContext):
55
def __init__(self, runtime):
56
self._cx = runtime.new_context()
57
self._cx.bind_class(NamespaceElement)
59
# namespace creation (global namespace)
61
def bindObject (self, name, aObject, aClass = None):
62
if aClass != None and aClass != False:
63
self._cx.bind_class (aClass)
64
self._cx.bind_object (name, aObject)
66
def bindFunction(self, name, object, asGlobal = False):
67
# asGlobal isn't supported at the moment
68
self._cx.bind_callable(name, object)
71
def buildMethod(self, name, code, parameters={}):
72
return ECMAscriptMethod(self, name, code, parameters)
75
def buildFunction(self, name, code, parameters={}):
76
return ECMAscriptFunction(self, name, code, parameters)
78
#class ECMAscriptMethod (Base.VirtualMethod):
79
class ECMAscriptMethod (Base.VirtualFunction): # a rename to fix it for the moment
81
def __init__(self, context, name, code, parameters):
82
Base.VirtualFunction.__init__(self, context, name, code, parameters)
83
# Take care of special names
84
self._name = string.replace(self._name,'-','_')
89
# TODO: add error handling
91
# build parameter list
94
for key in self._parameters.keys():
95
value = self._parameters[key]
96
param = param + delim + key
98
param = ',%s=%s' % (param, value)
102
self._realcode = '\n%s = function (%s) {%s};' % (self._name, param,
104
# name of helper function
105
self._hname = '__%s' % string.replace(self._name,'.','_')
107
# add helper function
108
self._realcode = '%s\nfunction %s (%s) { return %s(%s);}; ' % (self._realcode,
112
GDebug.printMesg(8, "Adding code to ECMAscript namespace :'%s'" % self._realcode)
113
# load code into context
115
self._cx.eval_script(self._realcode)
118
(group, name, message, detail) = errors.getException (1)
119
if group == 'system':
120
group = 'application'
121
raise language.CompileError, (group, name, message, detail)
124
def execute(self, *args,**params):
126
# TODO: find a way to pass parameter
128
#return self._cx.eval_script("x=%s(%s);" % (self._name,param))
129
# call function cannot call the function itself, so just simulate it
130
# by creating a shortcut function to call like its_me_function to call
131
return self._cx.call_fn(self._hname, ()) #args)
134
(group, name, message, detail) = errors.getException (1)
135
if group == 'system':
136
group = 'application'
137
raise language.RuntimeError, (group, name, message, detail)
139
def rebind(self, obj, name):
142
class ECMAscriptFunction(Base.VirtualFunction):
143
def __init__(self, context, name, code, parameters):
144
Base.VirtualFunction.__init__(self, context, name, code, parameters)
145
# Take care of special names
146
self._name = string.replace(self._name,'-','_')
151
# TODO: add error handling
153
# build parameter list
156
for key in self._parameters.keys():
157
value = self._parameters[key]
158
param = param + delim + key
159
# if value==None or value == 'None':
160
# param = '%s=%s' % (param, value)
164
self._realcode = '%s = function (%s) {%s};' % (self._name, param,
167
GDebug.printMesg(8, "Adding code to ECMAscript namespace :'%s'" % self._realcode)
168
# load code into context
170
self._cx.eval_script(self._realcode)
173
(group, name, message, detail) = errors.getException (1)
174
if group == 'system':
175
group = 'application'
176
raise language.CompileError, (group, name, message, detail)
179
def execute(self, *args,**params):
180
# TODO: check args for object instances
182
retval = self._cx.call_fn(self._name, args)
186
(group, name, message, detail) = errors.getException (1)
187
if group == 'system':
188
group = 'application'
189
raise language.RuntimeError, (group, name, message, detail)