slavapestov/factor — pull request #1045 by bjourne (2014-04-27 03:44:33+00:00)
Title: Alien ffi initializer

Here is a pr I've been working on. Basic problem:

Some vocabs (mostly alien ones) need some form of initialization before they can be used. But how and when should that happen?

It can't happen in a startup hook because then starting Factor becomes slower and the user might not have any interest in the library.

It can't happen when the vocab is USE:d. Because it's impossible to detect when a vocab you use has a transitive dependency on a vocab needing initialization. I thought adding an `on-vocab-load-hook` would be easy, but it wasn't. :)

The only option that works is to do it like the ssl vocabs and have a `maybe-init-ssl` word and then try to ensure that it is called before every usage of ssl. But it's really easy to forget to call the "maybe-init" word or accidentally depend on some other code to call it for you which then changes.

So the idea is to make initialization happen automatically and transparently to the one using the lib:
```factor
: init-ffi ( -- )
SSL_library_init ssl-error
SSL_load_error_strings
OpenSSL_add_all_digests
OpenSSL_add_all_ciphers ;

INITIALIZER: init-ffi
```
This would instrument all ssl functions to first call `maybe-call-initializer` which would ensure that `init-ffi` is called exactly once. So in a way it's a generalization of the `maybe-init-ssl` pattern.

What do you think? Maybe there's some better way to solve the problem?