Created
December 3, 2014 11:43
-
-
Save faried/23ad4b0f80266323fb3e to your computer and use it in GitHub Desktop.
using chibi-scheme in a nacl module: a rilly rilly simple example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
// Use of this source code is governed by a BSD-style license that can be | |
// found in the LICENSE file. | |
#include "ppapi/cpp/instance.h" | |
#include "ppapi/cpp/module.h" | |
#include "ppapi/cpp/var.h" | |
#include "chibi/eval.h" | |
class PN1Instance : public pp::Instance { | |
private: | |
sexp ctx; | |
public: | |
explicit PN1Instance(PP_Instance instance) : pp::Instance(instance) | |
{ | |
ctx = sexp_make_eval_context(NULL, NULL, NULL, 0, 0); | |
sexp_load_standard_env(ctx, NULL, SEXP_SEVEN); | |
sexp_load_standard_ports(ctx, NULL, stdin, stdout, stderr, 0); | |
} | |
virtual ~PN1Instance() { | |
sexp_destroy_context(ctx); | |
} | |
virtual void HandleMessage(const pp::Var& var_message) { | |
if (!var_message.is_string()) return; | |
std::string message = var_message.AsString(); | |
pp::Var var_reply; | |
sexp_gc_var2(ret, out); | |
sexp_gc_preserve2(ctx, ret, out); | |
// fixme | |
if (message.find("(") == 0) { | |
ret = sexp_eval_string(ctx, message.c_str(), message.size(), NULL); | |
out = sexp_write_to_string(ctx, ret); | |
var_reply = pp::Var(sexp_string_data(out)); | |
PostMessage(var_reply); | |
} | |
sexp_gc_release2(ctx); | |
} | |
}; | |
class PN1Module : public pp::Module { | |
public: | |
PN1Module() : pp::Module() {} | |
virtual ~PN1Module() {} | |
virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
return new PN1Instance(instance); | |
} | |
}; | |
namespace pp { | |
Module* CreateModule() { | |
return new PN1Module(); | |
} | |
} // namespace pp | |
// eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment