| OLD | NEW |
| 1 --- | 1 --- |
| 2 layout: default | 2 layout: default |
| 3 title: "Native Extensions for the Standalone Dart VM" | 3 title: "Native Extensions for the Standalone Dart VM" |
| 4 description: "Learn how to enable command-line Dart apps to call C/C++ functions
." | 4 description: "Learn how to enable command-line Dart apps to call C/C++ functions
." |
| 5 rel: | 5 rel: |
| 6 author: william-hesse | 6 author: william-hesse |
| 7 --- | 7 --- |
| 8 | 8 |
| 9 # {{ page.title }} | 9 # {{ page.title }} |
| 10 _Written by William Hesse <br /> | 10 _Written by William Hesse <br /> |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 {% endhighlight %} | 59 {% endhighlight %} |
| 60 | 60 |
| 61 The code that implements a native extension executes at two different times. Fir
st, it runs when the native extension is loaded. Later, it runs when a function
with a native implementation is called. | 61 The code that implements a native extension executes at two different times. Fir
st, it runs when the native extension is loaded. Later, it runs when a function
with a native implementation is called. |
| 62 | 62 |
| 63 Here is the sequence of events at load time, when a Dart app that imports sample
_synchronous_extension.dart starts running: | 63 Here is the sequence of events at load time, when a Dart app that imports sample
_synchronous_extension.dart starts running: |
| 64 | 64 |
| 65 1. The Dart library sample_synchronous_extension.dart is loaded, and the Dart VM
hits the code <b><code>#import("dart-ext:sample_extension")</code></b>. | 65 1. The Dart library sample_synchronous_extension.dart is loaded, and the Dart VM
hits the code <b><code>#import("dart-ext:sample_extension")</code></b>. |
| 66 2. The VM loads the shared library "sample_extension" from the directory contain
ing the Dart library. | 66 2. The VM loads the shared library "sample_extension" from the directory contain
ing the Dart library. |
| 67 3. The function sample_extension_Init() in the shared library is called. It regi
sters the shared library function ResolveName() as the name resolver for all nat
ive functions in the library sample_extension.dart. We'll see what the name reso
lver does when we look at synchronous native functions, below. | 67 3. The function sample_extension_Init() in the shared library is called. It regi
sters the shared library function ResolveName() as the name resolver for all nat
ive functions in the library sample_extension.dart. We'll see what the name reso
lver does when we look at synchronous native functions, below. |
| 68 | 68 |
| 69 <aside class="note" markdown="1"> | 69 <aside> |
| 70 **Note:** | 70 <div class="alert alert-info"> |
| 71 <strong>Note:</strong> |
| 71 The filename of the shared library depends on the platform. On Windows, the VM l
oads sample_extension.dll, on Linux it loads libsample_extension.so, and on Mac
it loads libsample_extension.dylib. We show how to build and link these shared l
ibraries in an appendix at the end of the article. | 72 The filename of the shared library depends on the platform. On Windows, the VM l
oads sample_extension.dll, on Linux it loads libsample_extension.so, and on Mac
it loads libsample_extension.dylib. We show how to build and link these shared l
ibraries in an appendix at the end of the article. |
| 73 </div> |
| 72 </aside> | 74 </aside> |
| 73 | 75 |
| 74 ###Using the Dart Embedding API from native code | 76 ###Using the Dart Embedding API from native code |
| 75 | 77 |
| 76 As the sample extensions show, the native shared library contains an initializat
ion function, a name resolution function, and the native implementations of func
tions declared as native in the Dart part of the extension. The initialization f
unction registers the native name resolution function as responsible for looking
up native function names in this library. When a function declared as **<code>n
ative "<em>function_name</em>"</code>** | 78 As the sample extensions show, the native shared library contains an initializat
ion function, a name resolution function, and the native implementations of func
tions declared as native in the Dart part of the extension. The initialization f
unction registers the native name resolution function as responsible for looking
up native function names in this library. When a function declared as **<code>n
ative "<em>function_name</em>"</code>** |
| 77 in the Dart library is called, the native library's name resolution function is
called with the string "<em>function_name</em>" as an argument, plus the number
of arguments in the function call. The name resolution function then returns a f
unction pointer to the native implementation of that function. The initializatio
n function and the name resolution function look pretty much the same in all Dar
t native extensions. | 79 in the Dart library is called, the native library's name resolution function is
called with the string "<em>function_name</em>" as an argument, plus the number
of arguments in the function call. The name resolution function then returns a f
unction pointer to the native implementation of that function. The initializatio
n function and the name resolution function look pretty much the same in all Dar
t native extensions. |
| 78 | 80 |
| 79 The functions in the native library use the Dart Embedding API to communicate wi
th the VM, so the native code includes the header <b>dart_api.h</b>, which is in
the SDK at dart-sdk/include/dart_api.h or in the repository at [runtime/include
/dart_api.h](http://dart.googlecode.com/svn/trunk/dart/runtime/include/dart_api.
h). The Dart Embedding API is the interface that embedders use to include the Da
rt VM in a web browser or in the standalone VM for the command line. It consists
of about 100 function interfaces and many data type and data structure definiti
ons. These are all shown, with comments, in dart_api.h. Examples of using them a
re in the unit test file [runtime/vm/dart_api_impl_test.cc](http://code.google.c
om/p/dart/source/browse/trunk/dart/runtime/vm/dart_api_impl_test.cc). | 81 The functions in the native library use the Dart Embedding API to communicate wi
th the VM, so the native code includes the header <b>dart_api.h</b>, which is in
the SDK at dart-sdk/include/dart_api.h or in the repository at [runtime/include
/dart_api.h](http://dart.googlecode.com/svn/trunk/dart/runtime/include/dart_api.
h). The Dart Embedding API is the interface that embedders use to include the Da
rt VM in a web browser or in the standalone VM for the command line. It consists
of about 100 function interfaces and many data type and data structure definiti
ons. These are all shown, with comments, in dart_api.h. Examples of using them a
re in the unit test file [runtime/vm/dart_api_impl_test.cc](http://code.google.c
om/p/dart/source/browse/trunk/dart/runtime/vm/dart_api_impl_test.cc). |
| 80 | 82 |
| 81 A native function to be called from Dart must have the type **Dart\_NativeFuncti
on**, which is defined in dart_api.h as: | 83 A native function to be called from Dart must have the type **Dart\_NativeFuncti
on**, which is defined in dart_api.h as: |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 1. Create a new project of type Win32/Win32 project in Visual Studio 2008 or 201
0. Give the project the same name as the native extension. On the next screen of
the wizard, change the application type to DLL and select "Empty project", then
choose finish. | 357 1. Create a new project of type Win32/Win32 project in Visual Studio 2008 or 201
0. Give the project the same name as the native extension. On the next screen of
the wizard, change the application type to DLL and select "Empty project", then
choose finish. |
| 356 2. Add the C/C++ files for the native extension to the source files folder in th
e project. Make sure to include the [extension name]_dllmain_win.cc file. | 358 2. Add the C/C++ files for the native extension to the source files folder in th
e project. Make sure to include the [extension name]_dllmain_win.cc file. |
| 357 3. Change the following settings in the project's properties: | 359 3. Change the following settings in the project's properties: |
| 358 1. Configuration properties / Linker / Input / Additional dependencies: Add d
art-sdk\bin\dart.lib, from the downloaded Dart SDK. | 360 1. Configuration properties / Linker / Input / Additional dependencies: Add d
art-sdk\bin\dart.lib, from the downloaded Dart SDK. |
| 359 2. Configuration properties / C/C++ / General / Additional Include Directorie
s: Add the path to the directory containing dart_api.h, which is dart-sdk/includ
e in the downloaded Dart SDK. | 361 2. Configuration properties / C/C++ / General / Additional Include Directorie
s: Add the path to the directory containing dart_api.h, which is dart-sdk/includ
e in the downloaded Dart SDK. |
| 360 3. Configuration properties / C/C++ / Preprocessor / Preprocessor Definitions
: Add DART_SHARED_LIB. This is just to export the <extension name>_init function
from the DLL, since it has been declared as DART_EXPORT. | 362 3. Configuration properties / C/C++ / Preprocessor / Preprocessor Definitions
: Add DART_SHARED_LIB. This is just to export the <extension name>_init function
from the DLL, since it has been declared as DART_EXPORT. |
| 361 4. Build the project, and copy the DLL to the correct directory, relative to the
Dart library part of the extension. Make sure to build a 32-bit DLL for use wi
th the 32-bit SDK download, and a 64-bit DLL for use with the 64-bit download. | 363 4. Build the project, and copy the DLL to the correct directory, relative to the
Dart library part of the extension. Make sure to build a 32-bit DLL for use wi
th the 32-bit SDK download, and a 64-bit DLL for use with the 64-bit download. |
| 362 | 364 |
| 363 | 365 |
| 364 | 366 |
| OLD | NEW |