
200
7
Integrating External Libraries
You can extend the JavaScript DOM for an application by writing a C or C++ shared library, compiling it for
the platform you are using, and loading it into JavaScript as an ExternalObject object
. A shared library is
implemented by a DLL in Windows, a bundle or framework in Mac OS, or a SharedObject in UNIX.
You can access the library functions directly through the
ExternalObject instance, or you can define an
interface that allows your C/C++ code to create and access JavaScript classes and objects.
All Adobe Creative Suite 4 applications support this feature.
Example code
The sample code distributed with the Adobe ExtendScript SDK includes an example that demonstrates
how write a C/C++ shared library to be integrated with JavaScript. It is in the directory:
sdkInstall/sdksamples/cpp/
The sample shows how to write a plug-in for Adobe Bridge in C/C++, using the ExternalObject
mechanism, which enables the C/C++ code to be called from the JavaScript context. Project files for
Microsoft Visual Studio 2005 and XCode 2.4 are included in subfolders of
sdkInstall/sdksamples/cpp/build.
Loading and using shared libraries
To load an external shared library into JavaScript, create a new ExternalObject object. The instance acts as
a container and manager for the JavaScript interface to the library. It provides a logging facility that prints
status information to the JavaScript Console in the ExtendScript Toolkit, to help you debug your external
library use.
Once the library has been loaded, its exported symbols become available to JavaScript. In your JavaScript
code, you can call the functions defined in the library directly in the
ExternalObject instance, or indirectly
through library-defined object types.
X Direct access to library calls through the ExternalObject instance — Use the direct-access style
for C-language libraries. For each function defined in the C library, there is a corresponding method in
the
ExternalObject object. You can pass data to these methods and receive the return value directly.
For example:
mylib = new ExternalObject ("lib:" + samplelib); // load the library
alert(mylib.version) ;
// access functions directly from ExternalObject instance
var a = mylib.method_abc(1,2.0,true, "this is data") ;
alert(a) ;
mylib.unload() ;
For details of how to define functions for direct access through the ExternalObject object, see
“
Defining entry points for direct access” on page 203.
X Indirect access to library calls through JavaScript classes — Use the indirect style to access classes
defined in a C++ library. For each C++ class defined in the library, a corresponding JavaScript class is
Comentarios a estos manuales