Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Communicating with a dynamic scripting language


D's features also lead to easy integration with dynamic languages. It can host a dynamic type as well as dynamic objects.

Getting ready

To begin, download bindings to a scripting language. Here, we'll use the one I wrote whose syntax is inspired by both D and JavaScript. It was written with the goal to blur the line between the D code and the script code—the scripting language's dynamic type can also be used in D itself. Download jsvar.d and script.d from the following website:

Build your program with all three files on the command line, as follows:

dmd yourfile.d jsvar.d script.d

How to do it…

Let's execute the following steps to communicate with JavaScript:

  1. Create an object to wrap the script engine with a friendlier API.

  2. Prepare the script engine by passing data and function references to it. It may be necessary to wrap D functions in transforming functions to match the script engine's layout.

  3. Use opDispatch and...