~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/logic/adapters/ecmascript.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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.
 
18
#
 
19
# Copyright 2003-2005 Free Software Foundation
 
20
#
 
21
#
 
22
# FILE:
 
23
# ECMAscript/Adapter.py
 
24
#
 
25
# DESCRIPTION:
 
26
# Provides a way to execute python code in a given environment
 
27
#
 
28
# NOTES:
 
29
#
 
30
import sys
 
31
import types
 
32
import string
 
33
 
 
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
 
39
 
 
40
try:
 
41
  from spidermonkey import Runtime
 
42
except:
 
43
  print 'Spidermonkey python module not installed!'
 
44
  print 'You can get it from http://wwwsearch.sourceforge.net/python-spidermonkey/!'
 
45
  sys.exit(0)
 
46
 
 
47
class LanguageAdapter(Base.BaseLanguageAdapter):
 
48
  def __init__(self):
 
49
    self._rt = Runtime()
 
50
    
 
51
  def createNewContext(self):
 
52
    return ExecutionContext(self._rt)
 
53
 
 
54
class ExecutionContext(Base.BaseExecutionContext):
 
55
  def __init__(self, runtime):
 
56
    self._cx = runtime.new_context()
 
57
    self._cx.bind_class(NamespaceElement)
 
58
 
 
59
  # namespace creation (global namespace)
 
60
  #
 
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)
 
65
 
 
66
  def bindFunction(self, name, object, asGlobal = False):
 
67
    # asGlobal isn't supported at the moment
 
68
    self._cx.bind_callable(name, object)
 
69
 
 
70
  # script / trigger /
 
71
  def buildMethod(self, name, code, parameters={}):
 
72
    return ECMAscriptMethod(self, name, code, parameters)
 
73
 
 
74
    # script / trigger /
 
75
  def buildFunction(self, name, code, parameters={}):
 
76
    return ECMAscriptFunction(self, name, code, parameters)
 
77
 
 
78
#class ECMAscriptMethod (Base.VirtualMethod):
 
79
class ECMAscriptMethod (Base.VirtualFunction):  # a rename to fix it for the moment
 
80
 
 
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,'-','_')
 
85
    self._cx=context._cx
 
86
    self.compile()
 
87
 
 
88
  def compile(self):
 
89
    # TODO: add error handling
 
90
 
 
91
    # build parameter list
 
92
    param = ''
 
93
    delim =''
 
94
    for key in self._parameters.keys():
 
95
      value = self._parameters[key]
 
96
      param = param + delim + key
 
97
      if value==None:
 
98
        param = ',%s=%s' % (param, value)
 
99
      delim = ','
 
100
 
 
101
    # build code
 
102
    self._realcode = '\n%s = function (%s) {%s};' % (self._name, param,
 
103
                                                   self._code);
 
104
    # name of helper function
 
105
    self._hname = '__%s' % string.replace(self._name,'.','_')
 
106
 
 
107
    # add helper function
 
108
    self._realcode = '%s\nfunction %s (%s) { return %s(%s);}; ' % (self._realcode,
 
109
                                                                  self._hname, param,
 
110
                                                                  self._name,param)
 
111
 
 
112
    GDebug.printMesg(8, "Adding code to ECMAscript namespace :'%s'" % self._realcode)
 
113
    # load code into context
 
114
    try:
 
115
      self._cx.eval_script(self._realcode)
 
116
      
 
117
    except:
 
118
      (group, name, message, detail) = errors.getException (1)
 
119
      if group == 'system':
 
120
           group = 'application'
 
121
      raise language.CompileError, (group, name, message, detail)
 
122
 
 
123
 
 
124
  def execute(self, *args,**params):
 
125
    param = ""
 
126
    # TODO: find a way to pass parameter
 
127
    try:
 
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)
 
132
 
 
133
    except:
 
134
      (group, name, message, detail) = errors.getException (1)
 
135
      if group == 'system':
 
136
          group = 'application'
 
137
      raise language.RuntimeError, (group, name, message, detail)
 
138
 
 
139
  def rebind(self, obj, name):
 
140
    pass
 
141
 
 
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,'-','_')
 
147
    self._cx=context._cx
 
148
    self.compile()
 
149
 
 
150
  def compile(self):
 
151
    # TODO: add error handling
 
152
 
 
153
    # build parameter list
 
154
    param = ''
 
155
    delim =''
 
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)
 
161
      delim = ','
 
162
 
 
163
    # build code
 
164
    self._realcode = '%s = function (%s) {%s};' % (self._name, param,
 
165
                                                   self._code);
 
166
    
 
167
    GDebug.printMesg(8, "Adding code to ECMAscript namespace :'%s'" % self._realcode)
 
168
    # load code into context
 
169
    try:
 
170
      self._cx.eval_script(self._realcode)
 
171
 
 
172
    except:
 
173
      (group, name, message, detail) = errors.getException (1)
 
174
      if group == 'system':
 
175
        group = 'application'
 
176
      raise language.CompileError, (group, name, message, detail)
 
177
 
 
178
 
 
179
  def execute(self, *args,**params):
 
180
    # TODO: check args for object instances
 
181
    try:
 
182
      retval = self._cx.call_fn(self._name, args)
 
183
      return retval[0]
 
184
 
 
185
    except:
 
186
      (group, name, message, detail) = errors.getException (1)
 
187
      if group == 'system':
 
188
        group = 'application'
 
189
      raise language.RuntimeError, (group, name, message, detail)