FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
JSFunction.cpp
1 /**********************************************************\
2  Original Author: Richard Bateman
3 
4  Created: Dec 20, 2010
5  License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12  Copyright 2010 Richard Bateman, FaceBook inc, Firebreath development team
13 \**********************************************************/
14 
15 #include "precompiled_headers.h" // On windows, everything above this line in PCH
16 #include "JSFunction.h"
17 
18 FB::JSFunction::JSFunction( const FB::JSAPIWeakPtr& obj, const std::wstring& func, const FB::SecurityZone zone)
19  : FB::JSAPIAuto(zone, FB::wstring_to_utf8(func) + "()"), m_apiWeak(obj), m_methodName(FB::wstring_to_utf8(func))
20 {
21  init();
22 }
23 
24 FB::JSFunction::JSFunction( const FB::JSAPIWeakPtr& obj, const std::string& func, const FB::SecurityZone zone)
25  : FB::JSAPIAuto(zone, func + "()"), m_apiWeak(obj), m_methodName(func)
26 {
27  init();
28 }
29 
30 void FB::JSFunction::init()
31 {
32  m_allowMethodObjects = false;
33  // There are no reserved members on this object
34  m_reservedMembers.clear();
35 }
36 
37 FB::variant FB::JSFunction::exec( const std::vector<variant>& args )
38 {
39  FB::JSAPIPtr api = m_apiWeak.lock();
40  if (!api)
41  throw new FB::script_error("Invalid JSAPI object");
42  // Force calls to use the zone this function was created with
43  FB::scoped_zonelock _l(api, getZone());
44  return api->Invoke(m_methodName, args);
45 }
46 
47 FB::variant FB::JSFunction::call( const std::vector<variant>& args )
48 {
49  FB::VariantList list;
50  if (args.size() >= 1) {
51  list.insert(list.end(), args.begin()+1, args.end());
52  }
53  return exec(list);
54 }
55 
56 FB::variant FB::JSFunction::apply( const std::vector<variant>& args )
57 {
58  FB::VariantList list;
59  if (args.size() >= 2) {
60  list = args[1].convert_cast<FB::VariantList>();
61  }
62  return exec(list);
63 }
64 
65 
66 bool FB::JSFunction::HasMethod( const std::string& methodName ) const
67 {
68  if (methodName == "" || methodName == "apply" || methodName == "call") {
69  return true;
70  } else {
71  return FB::JSAPIAuto::HasMethod(methodName);
72  }
73 }
74 
75 FB::variant FB::JSFunction::Invoke( const std::string& methodName, const std::vector<variant>& args )
76 {
77  if (methodName == "") {
78  return exec(args);
79  } else if (methodName == "call") {
80  return call(args);
81  } else if (methodName == "apply") {
82  return apply(args);
83  } else {
84  return FB::JSAPIAuto::Invoke(methodName, args);
85  }
86 }
87 
88 bool FB::JSFunction::HasProperty( const std::string& propName ) const
89 {
90  if (propName == "call" || propName == "apply")
91  return false;
92  else
93  return FB::JSAPIAuto::HasProperty(propName);
94 }
95 
96 
97 
98 
virtual bool HasMethod(const std::wstring &methodName) const
Definition: JSAPI.h:287
virtual variant Invoke(const std::wstring &methodName, const std::vector< variant > &args)
Definition: JSAPI.h:489
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64
boost::weak_ptr< FB::JSAPI > JSAPIWeakPtr
Defines an alias for a JSAPI weak_ptr (you should never use a JSAPI* directly)
Definition: APITypes.h:88
int SecurityZone
Used to set a SecurityZone for a method or property – used by JSAPIAuto.
Definition: APITypes.h:275
virtual bool HasProperty(const std::string &propertyName) const
Query if 'propertyName' is a valid property.
Definition: JSAPIAuto.cpp:162
virtual bool HasMethod(const std::string &methodName) const
Query if the JSAPI object has the 'methodName' method.
Definition: JSAPIAuto.cpp:146
boost::shared_ptr< FB::JSAPI > JSAPIPtr
Defines an alias for a JSAPI shared_ptr (you should never use a JSAPI* directly)
Definition: APITypes.h:94
Provides a helper class for locking.
Definition: JSAPI.h:548
Exception type; when thrown in a JSAPI method, a javascript exception will be thrown.
Definition: JSExceptions.h:28
std::string wstring_to_utf8(const std::wstring &src)
Accepts a std::wstring and returns a UTF8-encoded std::string.
Definition: utf8_tools.cpp:37
virtual variant Invoke(const std::string &methodName, const std::vector< variant > &args)
Called by the browser to invoke a method on the JSAPI object.
Definition: JSAPIAuto.cpp:329
virtual bool HasProperty(int idx) const
Query if the property at "idx" exists.
Definition: JSAPIAuto.cpp:178