Author Archives: paulvick

JsRT samples updated for Windows 8.1 RTM

As a few people have noticed, we took a small breaking change to the JsRT API between the Windows 8.1 Preview and Windows 8.1 RTM. The JsCreateFunction API got an additional parameter that allows registering a piece of data (which can just be null) with the native callback which will be provided to the callback when it’s invoked. It’s a super-useful place to stash something like a “this” pointer for a C++ callback, which is why we added it before the concrete completely dried on the API. It does mean, though, that most applications compiled against the preview will break and need to be recompiled against RTM.

Accordingly, we’ve updated both the MSDN documentation for JSRT and the Code Gallery hosting sample.

You can always reach me on Twitter here.

JsRT: Handling exceptions

The last major conceptual topic to cover for JsRT is how exceptions are handled.

Internally, Chakra uses C++ exception handling to handle both JavaScript exceptions and internal engine exceptions. However, even though the host might also be using C++ exception handling, the JsRT API surface enforces a strict separation between the host and Chakra when it comes to exceptions. Chakra exceptions will never cross over into the host (i.e. they will always be caught at the API boundary), and host exceptions should never cross over into the Chakra engine. Thus, hosts need to guard any callbacks to ensure that internal host exceptions don’t escape into Chakra.

OK, so, then how do exceptions work?

When a JavaScript exception occurs during script execution or during script compilation, and the exception is not handled in the script itself (i.e. the exception makes it to a JsRT API boundary), the containing runtime is put into an exception state. While in the exception state, no code can run in the runtime and all API calls will fail (with the error code JsErrorInExceptionState). This will continue until the host retrieves and clears the exception using the JsGetAndClearException API. This API will return an object representing the JavaScript value thrown (or the compile exception if the error occurred during compilation), and the runtime will be free to run code. How the host handles the exception is up to the host.

If the host is in the middle of a callback from Chakra, the host can also just return from the callback without clearing the runtime from an exception state. This will cause the JavaScript exception to be re-thrown when control passes back to the JavaScript engine.

If the host is in the middle of a callback and wants to “throw” a JavaScript exception, it can set the runtime into an exception state using JsSetException and then return from the callback. Just as above, the exception will be thrown in JavaScript once control passes back to the Chakra engine.

You should also follow me on Twitter here.

JsRT: Memory management

Yikes! It’s been longer than I thought it’s been since I last wrote on JsRT. Sorry about that, we’ve been a little busy.

With the two major concepts, runtimes and contexts, covered, there were a few additional things that need to be addressed for hosts that want to use JsRT. Probably the most important is how memory management is coordinated between Chakra and the host.

As you know, JavaScript is a garbage collected language, and everything you allocate through the JsRT API (with the exception of runtimes) is managed by the Chakra garbage collector. References will continue to live only as long as the garbage collector thinks they are “alive.” Once the GC thinks a reference is dead, the next collection will free up the memory and the reference will become invalid. The garbage collector looks in two places when determining if a reference is alive:

  1. The runtime’s garbage collected heap.
  2. The stack of the runtime’s current thread.

So a reference to a JavaScript value that is stored inside of another JavaScript value or in a local variable on the stack will always be seen by the garbage collector. But if a host stores a reference somewhere where the GC isn’t going to look (for example, in a host-allocated heap), then the reference will not be seen by the garbage collector and may result in premature collection of values that are still in use by the host. (When relying on a stack reference to keep a reference alive, you have to be extra careful–some language compilers (such as VC++) will optimize away stack variables where possible.)

If a host is going to store a reference where the GC can’t see it, it will need to manually add a reference to the object itself by calling JsAddRef. This will ensure the object will not go away until JsRelease is called. Calls to JsAddRef/JsRelease have to be paired–unbalanced calls will result in objects being collected too soon or staying alive forever.

You should also follow me on Twitter here.

JsRT: Execution Contexts

Last time, I covered runtimes, this time I want to briefly cover execution contexts. While a runtime owns all the machinery needed to execute code (heap, GC, JIT, etc.), it can’t just execute code on its own–it has to execute the code within a particular context. Each context shares its resources with other contexts in the runtime, but it has one crucial thing all to itself–the global object and everything that sits under it. So while code executed in two contexts that belong to the same runtime may use the same heap, they are nonetheless unable to see or affect one another at the language level. Contexts are effectively totally isolated from each other. Because objects created in a context are tied to that context’s unique global object, an object created in one context cannot be passed in to another context. The JsRT APIs check all incoming objects to ensure that they match the context being used and will error if they don’t.

To simplify the JsRT API, most of the APIs assume an ambient execution context. Once you set the current context on a thread using JsSetCurrentContext, all APIs will assume that context until you call JsSetCurrentContext again (APIs will return a JsErrorNoCurrentContext error if there isn’t a context that’s been set current on the thread). Contexts are tied to their parent runtime’s thread–making a context current on a thread means that that runtime becomes current on that thread. If the runtime happens to be active on another thread at the time, then setting the current context will fail.

Within a thread, however, calls into contexts can be nested. So, for example, a runtime r1 can create two contexts c1 and c2. It can then run a script in c1 which calls back into the host. The host callback can then set c2 as the current context and run some more code, provided that it restores c1 as the current context when it’s done. So it’s legal to run code in c2 while c1 is on the stack, you just have to make sure that you clean up when you’re done.

Finally, from a host’s perspective the most important thing to remember regarding contexts is that if you have host objects that you want to project into the JavaScript space for scripts to use, you have to do it for each context you create. There’s currently no way to say something like, “I want this object to appear in all contexts I create.”

You should also follow me on Twitter here.

JsRT: Runtimes

Now that the sample is out, I wanted to take a few posts to walk through some of the basics of hosting the Chakra engine in your application. Some of this will repeat the MSDN documentation a bit, but I’ll try and expand on some of the details.

The base entity in the JsRT API is a runtime. A runtime represents a complete JavaScript execution environment, including a garbage collected heap (with it’s own garbage collector) and just-in-time (JIT) native code compiler. So two different runtimes will have separate heaps and separate JITs, and will not share any memory or generated native code. They are entirely separate from one another.

Runtimes represent a single thread of execution, so more than one runtime can be active at the same time within a process, but within a runtime there will only ever be one active thread of execution. Runtimes are not tied to a particular system thread, however, but are rental threaded. That means that a runtime can move from one system thread to another provided that the runtime is not currently executing any code. So while a runtime is running code (or is in a host callback while running code), it cannot be moved to another thread. But if it’s not running anything, the host is free to use that runtime on another thread. This means that if a runtime isn’t active for some reason (maybe it’s waiting for something to happen), the thread is free to run something in another runtime.

When creating a runtime, a number of choices can be made about the behavior of the runtime. The most important is how background work will be done. By default, creating a runtime will create two additional threads: a GC thread for the garbage collector and JIT thread for the native code compiler. These threads allow garbage collection and JIT compilation to happen in the background while code is running in the foreground. There are two other configurations possible, though. If you don’t want any background work done, you can specify the JsRuntimeAttributeDisableBackgroundWork flag, and all GC and JIT work will be done on the foreground on demand. Or you can choose to run the background work yourself by passing in a JsThreadServiceCallback routine when you create the runtime. In that case, the runtime will call the callback when it has GC or JIT work that needs to be done. The callback can either run the work on any thread it likes, or it can return false, meaning that the work should just be run on the foreground thread. Using a callback allows a host to pool GC and JIT threads across runtimes, for example.

Code that is running inside a runtime can be interrupted by calling JsDisableRuntimeExecution. The way that interruption works is that all code compiled by the runtime inserts calls to a check routine that will fail if execution has been disabled. So code will continue to run, even after the disable API has been called, until it hits one of these checks. By default, these checks are fairly conservative to avoid impacting performance too much. In particular, we don’t insert checks in loops by default. This means you don’t have any additional overhead when running a tight loop, but it also means that if you happen to hit an infinite loop, your script will hang and will not be interruptible. If you specify the JsRuntimeAttributeAllowScriptInterrupt flag, the runtime will insert additional script interruption checks into places like the bottom of loops so that script interruption becomes more reliable, at the cost of some small amount of performance.

There are some bookkeeping tasks that the runtime may need to do, particularly in longer-running runtimes. For example, if a lot of memory has been freed up by the GC, it can be advantageous to decommit memory pages that are no longer in use, freeing them up for use by the system. This kind of work is what we consider “idle” work, and if your host is long running enough, there can be benefit in passing in the JsRuntimeEnableIdleProcessing flag and then periodically calling the JsIdle API when the host is idle. If idle processing isn’t enabled and we need to do some bookkeeping work, we’ll typically do that when a script finishes running.

And, finally, there may be situations where a host might want to turn off some of the runtime capabilities for safety or security. If you don’t want scripts you run to be able to dynamically run code (i.e. call eval or the function constructor), you can specify the JsRuntimeAttributeDisableEval flag. If you don’t want the JIT compiler to generate native code, and instead just run everything under the interpreter (for example, if you have some concern about native code generation in your process), you can specify the JsRuntimeAttributeDisableNativeCodeGeneration flag. This last flag should be used with a great deal of caution–as you can imagine, JITted code is vastly more efficient than interpreted byte codes, so you’ll pay a significant penalty for turning off the JIT. Use only when absolutely necessary.

The last thing you can choose when creating a runtime is the version of the language you want. This is fairly straightforward–you can pick IE10 (corresponds to ES5), IE11 (ES5 + some ES6 features + some compatibility features like __proto__), or “edge” which will give you the latest version. The edge version will float, so be warned–if your host is run on some future platform with some version higher than IE11 (whatever that will be), you’ll get that newest version. Caveat emptor.

I think that about covers it for runtimes. Up next will be execution contexts…

You should also follow me on Twitter here.

JsRT Samples and Documentation

After sorting out some kinks in the process, the samples and documentation for JsRT (the new hosting API for the Chakra JavaScript engine) are now online. You can find them here:

There’s one more sample I need to get up that shows how to map the heap enumeration API to JSON such that Visual Studio 2013 preview can open it in its memory analysis tool. That should be coming shortly. I’ve also got a few other things in the pipeline. If there’s anything in particular you’d like to see, leave a comment or hit me up on Twitter!

You can always reach me on Twitter here.

Introducing JsRT – Embedding JavaScript on Windows

One of the most exciting things about the JavaScript language in the past few years has been the way it which it has been making a leap beyond the browser. The world generally under-appreciates the power of small, high-embeddable, and interactive languages, and it’s been heartening to see the great work of the node.js crew and others using JS for a wide variety of non-browser application. Lots of applications and platforms have hidden value inside of them that is almost entirely inaccessible until someone like JavaScript comes along and unlocks them, enabling regular people to do things that can almost seem magical. (I saw a lot of this early on in my career working on Access, which combined the power of a relational database with embedded BASIC.) It’s what drew me to the Chakra JavaScript runtime team a little over a year ago, when I was given the opportunity to explore some ideas around making JavaScript embeddable on the Windows platform.

Actually, I can hear some of you saying, “Hey, wait a minute! You can already embed JavaScript on Windows! You just use the IActiveScript APIs. Everybody knows that!” That’s true, but the IActiveScript APIs have two problems:

  1. They are COM based and require extensive use of IDispatch for communication between the JavaScript host and JavaScript code (which is an issue both in terms of coding and performance).
  2. They only work with the JavaScript engine that last shipped with IE8 (a.k.a. the “legacy engine”). The new Chakra engine, which has a high-performance GC and JIT compiler, does not officially support the IActiveScript interfaces. It talks to IE using a set of private (and IE-specific) interfaces.

However, I’m proud to announce that, as of the IE11 preview (released at build last week), Chakra now has a fully supported public API! It’s called the “JsRT API” (for “JavaScript RunTime API”) and it enables any application running on Windows to use the Chakra JavaScript engine. The APIs are all  regular Win32 APIs and are designed for ease-of-use and high-performance applications. Here are some technical details:

  • The JsRT APIs are part of IE11 and will be supported on any operating system that has IE11 installed on it. RIght now, that means Windows 8.1 and Windows Server 2012 R2. Once IE11 is released for them, it will also mean Windows 7 and Windows Server 2008 R2.
  • The Windows 8.1 SDK (included with the Visual Studio 2013 preview) includes the new header file jsrt.h and export library file jsrt.lib which you can use to compile against in C/C++ projects.
  • We don’t have an official CLR wrapper at this point, but we will be providing a code sample shortly that wraps all the JsRT APIs with P/Invoke calls.
  • The JsRT APIs are not supported in Windows Runtime applications. Because the Windows Runtime itself hosts Chakra, there are a number of important technical issues having to do with the way the system hosting and application hosting could interact that need to be solved. We won’t have solutions for these issues in the Windows 8.1 timeframe, unfortunately.

We’ll also be putting up code samples that show off all the basics of hosting Chakra, including some side benefits that you get for free from the rest of the Microsoft ecosystem:

  • The Visual Studio script debugger (or, really, any IActiveScript debugger) will work with a Chakra host. This includes breakpoints, locals inspection, stepping, etc. as well as some exciting new features for IE11 that I’ll talk about later.
  • The Visual Studio 2013 preview profiler will work with a Chakra host out of the box to profile JavaScript code.
  • The Visual Studio 2013 preview JavaScript memory analyzer will work with a Chakra host as well.

As you can see, there are a lot of capabilities available when you use Chakra inside of your application. This is just the introductory post, I’ll be talking in more detail in the coming weeks about all the cool things you can do with the runtime. But in the meantime, download the Windows 8.1 preview and the Visual Studio 2013 preview and get cracking!

You can always reach me on Twitter here.

The Story of the Bill Gates Quote

Recently I told the story of my one product review with Bill Gates, and I said that there was one other terrifying encounter I had with him (well, really, his world) but it would have to wait for another day. As luck had it, I was talking with someone a couple of nights ago who works in Bill’s extended universe and ended up relating my second story, so I thought I’d go ahead and share it here too.

Those with long memories may know that I wrote a book on Visual Basic that came out around the time of Visual Studio 2003. Here’s a picture of it, from Amazon:

Book Cover

If you look up there at the top of the book cover, you’ll see a teeny quote that reads:

Visual Basic is a cornerstone of Microsoft’s development tools–and this book is a key language reference for Visual Basic. — Bill Gates, Chairman, Microsoft Corporation

How exciting! Bill f**king Gates read my book! Well, not really. Here’s what happened…

It all started when my publisher said that they were going to be meeting with Steve Ballmer to discuss the .NET book series that my book was a part of. They said that they were going to ask if maybe Steve would be willing to write a forward for my book since BASIC has such a long and storied history at Microsoft. I said that sounded great and then it hit me that I was friends with Bill Gate’s technical assistant at the time, and so I said that, hey, maybe I could see about getting a forward from Bill himself (since of course, BASIC was his first product!). They thought that was a great idea, of course, so off I went.

My friend directed me to Bill’s writer (the guy who did speeches, etc.) and I had a nice chat with him through email. He said there was basically no way that Bill would do a forward since, as you can imagine, he was getting millions of requests to write forwards and they just had a blanket policy of saying “no.” In talking a little, he did say that he thought it would be OK to do a quote for the cover, assuming that everyone understood that: a) this was a one time thing, and b) not to go around talking about how Bill gave me a quote for my book, since they didn’t want to open the floodgates or anything. He thought it was a small enough thing that I could just come up with the quote, he’d wordsmith it and approve it, and we’d be done–no need to get Bill involved in something so small. Great! I told my publisher and they were ecstatic.

Then one morning I come into work and I’ve got an email in my inbox from someone in my VP’s office saying, “I need to talk to you about a quote your publisher is getting from Bill? Do you know anything about this?”

Oh, s**t.

You see, if there’s one thing I’ve learned about powerful people, it’s that they tend to have an “external immune system” made up of people who’s job it is to filter out the avalanche of requests, demands, pleadings, and just plain crazies who want to get some part of them. It’s just a fact of life. And, furthermore, if you’re dealing with a powerful person you do not want to trip this immune response because it can get pretty tense pretty quickly, especially when you happen to be working for the company that they run. Which it became clear pretty quickly was exactly what I had done.

What had happened was that someone high up at my publisher had met with Bill for some reason or another, and in conversation had thanked him for providing the quote for my book. At which point, Bill apparently said, “What quote?” And BOOM! the immune response started gearing up, trying to figure out exactly where this “quote” had come from and who exactly it was who was claiming to speak for one of the richest men on the planet. Within hours, I was in conversation with senior people at my publisher, with Bill’s personal assistant, with my VP’s personal assistant, with my VP himself, and with Bill’s writer (who, understandably, was not pleased that I had managed to screw up this favor he did me). In the end, I profusely apologized, the quote was vetted a bit more, the immune response quieted down, and the book got published. But it was very tense there for a moment. I really didn’t want to be known to upper management only as “the guy who tricked Bill Gates into giving him a quote for his stupid book.”

Suffice it to say, time has gone on. I moved on from VB, Bill’s moved on to other pursuits (as has my old VP), and the book, while it did OK, was never updated past 2003. But just reading the old emails, which I did in preparation for writing this, got my heart going again just a bit. Not something I think I’d ever want to repeat…

You should also follow me on Twitter here.

My (Terrifying) Meeting with Bill Gates

This being Halloween and all, I thought I would relate one of the most frightening experiences I’ve had in my two decades working at Microsoft. I was reminded of it this weekend when we had a small reunion for everyone who’s worked on Access over it’s 25-or-so year history–it was a bit of old home week, seeing people who in some cases I haven’t seen in well over a decade and a half.

Anyway, it reminded me of an old practice at Microsoft called the “BillG review,” which was a (I think) twice-yearly meeting every major product team had with Bill Gates. They’d go over their progress since the last meeting, talk about their future plans, and give him the chance to ask questions and give direction. As one can imagine, this was a really huge deal (especially back in the days when Microsoft was still a 12,000 person company). A bad BillG review could be extremely traumatic, especially since Bill was not particularly known for his warm-and-fuzziness, nor did he suffer fools gladly. It could also radically alter product plans, depending on whether he agreed with what you were doing or not.

For most of my time in Access, I was too junior to actually attend these reviews, much less give any part of the presentation. I’d mostly just hear about it in the form of a flurry of design changes after each one. But by the time we’d gotten to working on Access ’97, a large majority of the senior folks had split off from the team to work on an ill-fated rewrite of Access. The main focus of Access ’97 was doing something about all the performance we’d lost moving to 32-bit (i.e. Windows 95) and integrating COM and VBA for the first time, and I had self-elected myself to be in charge of the development side of that effort. So when it came time to do the BillG review, I was tapped to give part of the presentation on the performance work. I was also there to throw to the lions in the eventuality that Bill started drilling in on some highly technical question, as he was famous for doing (c.f. Joel’s discussion of his first BillG review).

So the day of the review rolls around and I show up at Bill’s executive conference room with the rest of the team and various Access leaders. Of course, Bill’s running late, so Tod Nielsen (who was Access business unit manager at the time, I believe) decides to entertain us with colorful stories of BillG reviews past. And he decides to tell us the story of the final Omega BillG review.

Now, Omega was the desktop database project that preceded Access. They worked on it for about a year and a half (I think) before it got cancelled, all the code was thrown out, and they restarted on a new project that became Access. I wasn’t around for Omega, but I had heard lots of horror stories about it getting cancelled from people who’d been on that team. As you can imagine, then, the final BillG review for Omega was probably not a particularly happy event.

As I remember Tod telling it, he said that they were going through a list of what wasn’t going well with Omega when, all of a sudden, Bill loses it and starts swearing. “Get f–king recruiting in here, I want f–king recruiting in here right now!” Everyone’s a bit puzzled (and worried), and so they say, “OK, Bill, why do you want recruiting?” He replies, “Because I want to find out what f–king colleges we recruited you guys from and tell them not to f–king recruit there any more because they clearly produce f–king idiots!” Ouch. At that point, the team knew the review was over, so I they basically said, “All right, Bill, we’ll let you calm down and talk to you later,” and left. Tod thought the whole thing was hilarious… now. (It’s also possible he embellished the story a bit, I can’t testify to the veracity of his tale…)

Of course, as the person who was about to present about the primary feature of Access 97 to Bill f–king Gates, I was absolutely terrified. Great, I thought, I’m totally screwed. I’m going to die. Thankfully, Bill showed up, we did the review, and aside from one tense moment, everything went extremely smoothly. Then I got to sit and listen while Bill and the VPs sat around for a little while and discussed when they were going to merge our division in with Office, like they were moving pieces around on a chess board. Fascinating.

Not coincidentally, my other “scariest story” from my time at Microsoft also involves Bill Gates, but that’s a story for another time…

You should also follow me on Twitter here.