Added clang support. A lot of the motion here is introducing a new offsetof because clang's seems broken?
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2004-2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * SourcePawn. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_sourcepawn3_api_h_
20 #define _include_sourcepawn3_api_h_
24 #include "../include/keima_util.h"
40 * A Handle is a safe pointer that holds an object reference, such that it
41 * can be scanned and potentially moved by the garbage collector.
47 template <typename S> void typeCheck() {
62 template <typename S> Handle(Handle<S> other)
63 : obj_(reinterpret_cast<S *>(*other))
68 explicit Handle(T *obj)
73 T *operator ->() const {
76 T *operator *() const {
79 operator bool () const {
88 * A local variable that roots an object. Locals are owned by the nearest
89 * HandleScope, and as soon as that HandleScope dies, all its Locals are
90 * no longer available.
93 class Local : public Handle<T>
101 template <typename S> Local(Local<S> other)
102 : Handle<T>(reinterpret_cast<T *>(*other))
104 this->template typeCheck<S>();
107 template <typename S> Local(S *other)
114 * A non-local variable that outlives HandleScopes. A Root is reference-
115 * counted. When its reference count reaches 0, the Root is removed.
117 template <typename T>
118 class Root : public Handle<T>
126 Root<T>(const Root<T> &other)
127 : Handle<T>(reinterpret_cast<T *>(*other))
132 explicit Root(ke::HeapRoot *obj)
133 : Handle<T>(reinterpret_cast<T *>(obj))
138 static inline Root<T> New(Handle<T> other);
144 Root<T> &operator =(const Root<T> &other) {
146 this->obj_ = other.obj_;
157 * A HandleScope is where Local roots are allcoated. When the HandleScope
158 * is destroyed, all Handles created under it are also destroyed. HandleScopes
159 * may only be allocated on the stack, in LIFO order.
165 ke::Object **position_;
168 HandleScope(ke::Zone *zone, ke::Object **position, HandleScope *prev)
179 static ke::Object **CreateHandle(ke::Object *obj);
182 // Disallow silly stuff.
183 HandleScope(const HandleScope &);
184 void operator =(const HandleScope &);
185 void *operator new (size_t size);
186 void operator delete(void *, size_t);
191 // Error messages are pumped through this interface.
192 class IMessageReporter
195 virtual void onOutOfMemory() {
197 virtual void onUncaughtException(const char *message) {
199 virtual void beginCompilationErrors(const char *file) {
201 virtual void reportCompilationError(const char *message, unsigned line, unsigned col) {
203 virtual void endCompilationErrors() {
207 // For the purposes of error reporting, very basic information can be queried
208 // about the stack frame. This structure may be used to retrieve such
209 // information. During iteration, it is illegal to re-enter any scripted code
210 // or call any Keima functions that may cause garbage collection.
211 class IStackFrameIterator
214 virtual ~IStackFrameIterator() { }
216 // Returns true if there are more frames, false otherwise.
217 virtual bool done() = 0;
219 // Returns the name of the current function. This may be NULL if the
220 // function is anonymous.
221 virtual const char *function() = 0;
223 // Returns the file of the current function. This will return an invalid
224 // path for C++ functions (for example, "<native>").
225 virtual const char *source() = 0;
227 // Returns the line number currently executing on this frame. For C++
228 // functions, this is always 0.
229 virtual unsigned lineNumber() = 0;
231 // Advance to the previous frame in the call stack.
232 virtual void prev() = 0;
235 // A string is an immutable UTF8 buffer.
236 class KE_EXPORT String
239 // Returns the length of the string, in bytes.
242 // Returns the character buffer of the string, or NULL on failure. If the
243 // string is interned, chars() is guaranteed to succeed.
247 // A function is an executable unit of code.
248 class KE_EXPORT Function
251 // Returns the name of a function, which may be NULL. If not NULL, the
252 // string is guaranteed to be interned.
253 Local<String> name();
256 // A native is an ISourcePawnEngine-compatible function provided by C++.
257 class KE_EXPORT Native
260 // Returns the name of a native, which is never NULL. The string is
261 // guaranteed to be interned.
262 Local<String> name();
264 // Returns the function pointer of the native.
267 // Sets the function pointer of the native.
268 void bind(void *function);
271 // A module represents a single compiled file, and contains functions and
272 // global variables for that file (as well as exported types).
273 class KE_EXPORT Module
276 // Returns publics that are based on forwards.
277 unsigned numPublics();
278 Local<Function> getPublic(unsigned index);
281 unsigned numNatives();
282 Local<Native> getNative(unsigned index);
284 // Sets the native identity pointer, which is passed as the first argument
286 void setNativeIdentity(void *ptr);
293 * Initializes the Keima engine. This must be called exactly once before any
294 * other Keima functions are called. After this, it is safe to use Keima
295 * on the calling thread.
297 static bool Initialize();
300 * Shuts down the Keima engine and frees data structures on all threads.
302 static void Shutdown();
305 * Searches the call stack and returns true if the given module has any
306 * active code running. This is an O(n) algorithm.
308 static bool IsInCallStack(Handle<Module> module);
311 * Reclaim as much memory as possible. This pauses the calling thread
312 * until garbage collection completes.
314 static void CollectGarbage();
317 * Clear any pending exception.
319 static void ClearPendingException();
322 * Type checks a function against given parameters.
324 static bool CheckInvokeTypes(Handle<Function> fun, const Parameter *params, unsigned nparams);
327 * Executes a function with the given parameters. This interface is
328 * intended for compatibility with SourceMod's ISourcePawnEngine2 and
329 * should not be used otherwise, since it is intended to support only
330 * a small subset of the core language.
332 static bool Invoke(Handle<Function> fun, const Parameter *params, unsigned nparams, int *result);
335 * Sets the global message handler through which errors will be reported
337 static void InstallMessageReporter(IMessageReporter *reporter);
339 static IStackFrameIterator *NewStackFrameIterator();
343 * Suppresses exceptions. This is useful for calling API functions. When this
344 * in scope, errors will be reported, but the exception will be cleared after.
346 class AutoClearExceptions
349 ~AutoClearExceptions()
351 Keima::ClearPendingException();
355 // Argument-passing interface for Invoke().
378 Parameter(int *i, bool writeback)
380 writeback_(writeback)
391 Parameter(float *f, bool writeback)
393 writeback_(writeback)
398 Parameter(Type type, void *buffer, size_t length, bool writeback)
400 writeback_(writeback),
403 assert(type == Array || type == CharArray);
412 assert(type() == Int);
416 assert(type() == Float);
420 assert(type() == IntRef);
424 assert(type() == FloatRef);
427 bool writeback() const {
428 assert(type() >= IntRef && type() <= CharArray);
431 size_t length() const {
434 void *buffer() const {
435 assert(type() == Array || type() == CharArray);
454 * Compiles a file and returns a Module.
456 extern Local<Module> Compile(const char *path);
460 template <typename T> friend class Root;
463 static ke::HeapRoot *NewHeapRoot(ke::Object **objp);
464 static void IncRef(ke::HeapRoot *root);
465 static void DecRef(ke::HeapRoot *root);
468 template <typename T> Root<T>
469 Root<T>::New(Handle<T> other)
473 ke::Object **ptr = reinterpret_cast<ke::Object **>(*other);
474 return Root<T>(Internals::NewHeapRoot(ptr));
477 template <typename T> void
482 ke::HeapRoot *ptr = reinterpret_cast<ke::HeapRoot *>(**this);
483 Internals::IncRef(ptr);
486 template <typename T> void
491 ke::HeapRoot *ptr = reinterpret_cast<ke::HeapRoot *>(**this);
492 Internals::DecRef(ptr);
497 #endif // _include_sourcepawn3_api_h_