summaryrefslogtreecommitdiff
path: root/java/src/json/ext/ParserService.java
blob: b6015f96439a5841c739fa91ed9c4e056c81bdb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * This code is copyrighted work by Daniel Luz <dev at mernen dot com>.
 *
 * Distributed under the Ruby license: https://www.ruby-lang.org/en/about/license.txt
 */
package json.ext;

import java.io.IOException;
import java.lang.ref.WeakReference;

import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.runtime.load.BasicLibraryService;

/**
 * The service invoked by JRuby's {@link org.jruby.runtime.load.LoadService LoadService}.
 * Defines the <code>JSON::Ext::Parser</code> class.
 * @author mernen
 */
public class ParserService implements BasicLibraryService {
    public boolean basicLoad(Ruby runtime) throws IOException {
        runtime.getLoadService().require("json/common");
        RuntimeInfo info = RuntimeInfo.initRuntime(runtime);

        info.jsonModule = new WeakReference<RubyModule>(runtime.defineModule("JSON"));
        RubyModule jsonExtModule = info.jsonModule.get().defineModuleUnder("Ext");
        RubyClass parserClass =
            jsonExtModule.defineClassUnder("Parser", runtime.getObject(),
                                           Parser.ALLOCATOR);
        parserClass.defineAnnotatedMethods(Parser.class);
        return true;
    }
}