2012/04/20


たぶんこれからガチャガチャ変わるので参考にはならない。まぁネタなので。あと殆ど tools/mruby の下にある物と同じ。
またAPIとか整理されてないので、コンパイルにはソースディレクトリを参照に含めたりする必要がある。
#include <mruby.h>
#include <mruby/proc.h>
#include <compile.h>

int
main() {
  int n;
  mrb_state* mrb;
  struct mrb_parser_state* st;
  char* code =
"[1,2,3].each {|x| " \
"  p x             " \
"}                 ";

  mrb = mrb_open();
  st = mrb_parse_string(mrb, code);
  n = mrb_generate_code(mrb, st->tree);
  mrb_pool_close(st->pool);
  mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value());
}

/* vim:set et ts=2 sts=2 sw=2 tw=0: */
mrb_open() で状態遷移マシンを作り、文字列をパースして構文木を作る(mrb_parse_string())。yaccによるAST。その構文木からジャンプ命令等の実行コードを吐いて(mrb_generate_code())実行する(mrb_run())。
まぁ一般的な奴ですね。これからいろいろ出来る様になるんだろうけど、Lightweight を歌ってるのでどの辺が線引きなのか難しそう。
MinGW32 だと以下の様にしてビルド出来た。
gcc -Iinclude -Isrc foo.c lib/ritevm.lib
Posted at by



2011/11/10


以前、「jekyllで始める簡単ブログ」という記事を書いたのですが
Big Sky :: Jekyllで始める簡単ブログ

GithubのユーザページでJekyllが使われる様になりました。 GitHub Pages Upgraded to Jekyll 0.5.0 - GitHub I just released Jek...

http://mattn.kaoriya.net/software/lang/ruby/20090409185248.htm
octopressというのを使うともっと簡単に出来る事が分かった。
Octopress

Octopress is a framework designed by Brandon Mathis for Jekyll , the blog aware static site generato...

http://octopress.org
imathis/octopress - GitHub

What is Octopress? Octopress is Jekyll blogging at its finest. Octopress sports a clean responsive t...

https://github.com/imathis/octopress
以下簡単な手順を示す。
このgithubリポジトリは基本的に、1ブログに対して1つcloneする。cloneしたら # rake install
する。octopressをシステムにインストールのではなく、このブログ用リポジトリにsassテーマをキャッシュさせる為の設定を行うだけなので安心してやって貰ってok。
次にgithubにブログを作るのであれば # rake setup_github_pages
を実行し、読み取り書き込み権限のあるgit URLを貼り付けて完了。
Windowsの場合はちょっとここで修正が必要で、config.rbとかRakefileに変なパスが書かれているので直す。実際にはpublic/c:/path/to/public...みたいなフルパスが重なっているはずなのでpublic...に直せばok。
これについては時間があったらパッチ書いてpull requestを送るつもり。

さて、これでブログを書く準備が出来ました。簡単すぎますね!

試しにsource/_posts/2011-10-17-github-jekyll.mkdというmarkdownのファイルを作ります。中身は以前ご紹介した内容と変わりませんが、ocotopressは幾らか拡張プラグインが入っているので色んな書き方が出来ます。
例えばgist tagプラグインを使って ---
layout: post
title: "githubとjekyllとoctopressで作るブログ"
date: 2011-07-03 5:59
comments: true
categories:
---
便利すぎる...

{% gist 1292428 %}
こう書いて # rake gen_deploy
すると
githubとjekyllとoctopressで作るブログ - ギッハブる日記

Recent Posts githubとjekyllとoctopressで作るブログ Github Repos Status updating... @mattn on Github Latest T...

http://mattn.github.com/blog/blog/2011/07/03/github-jekyll/
こんな記事が出来上がります。dateはファイル名じゃなくてdateの方が(設定されていれば)優先されるみたいですね。以後は記事を書いてrake den_deployするだけの簡単なお仕事です。
他にも、imageタグや、haml形式入力、html5 videoタグなど、色んなプラグインがあります。また、sassフォルダ内にあるsassファイルを弄れば簡単しかも柔軟なCSSを吐き出せます。
なお、_config.xmlを弄ってタイトルやサブタイトル、githubアカウント、twitterアカウントなんかを設定すると上述の様なサイトが簡単に出来上がります。
# ----------------------- #
#      Main Configs       #
# ----------------------- #

url: http://mattn.github.com/blog
title: "ギッハブる日記"
subtitle: ギッハブで作る開発日誌
author: mattn

気をつけておいて欲しいのは、github/jekyllはCGIで動いている訳じゃなくて、octopressが静的コンテンツを生成しているので、記事をアップする環境に依存する事。まぁここはbundleを使っているのでOSさえ変わらなければ大丈夫かと思います。Windowsからでも既述の様な修正を入れれば使えています。無料のgithubで簡単にしかもモダンなブログサイトを作りたい方は、一考してみてはどうでしょうか。
Posted at by



2011/07/28


最近earthquakeという、rubyで書かれていて端末上で動作するtwitterクライアントをWindowsで動かそうと色々弄ってます。作者のjugyoさんにコミットビットも貰ってwork-on-windowsブランチで作業してます。earthquake側の修正はだいたいイケてるはずなんですが、問題はreadlineという行編集ライブラリで問題が発生。
ちゃんと書くと、C言語で書かれたreadlineをwrapしているreadlineモジュールじゃなくて、rubyinstallerに標準添付されたPure RubyScriptなモジュール。中身はUNIXのコードとWindowsのコードが入り乱れていて、rbreadline.rbなんか8686行もある大作。

まぁPure RubyScriptでCの真似事をしようってんだからこうなるよね...って感じ。
ただバイト長とキャラクタ数と、文字幅の扱いが間違ってて、Windows-31JなDBCSなんかではちゃんと動かない。パッチ書いて「問題があるんだよ。気付いて!」ってつもりでpull request送ったらいきなりIssue trackerでcloseされてカチンと来たので「その態度はいかがなもんかと思う」的なコメントをした。そしたら「まずテストを書け」との事だったので書いた。

しかしながら、上記の間違いを直すとしてreadlineの正しい動きをテストするって難しい。関数単体なら出来るけどUIのテストはどの言語でも苦しむ。おまけにreadlineはreadline()メソッドを呼び出してる最中は、テスターが止まってしまう。終えるにはユーザの入力が必要。
じゃぁユーザの入力を横取りしてやんよ!って事で、rl_get_char()をぶんどる事にした。
module RbReadline
  #...

  alias :old_rl_get_char :rl_get_char
  def rl_get_char(*a)
    c = old_rl_get_char(*a)
    c.force_encoding("ASCII-8BIT"if c
    @last_xy = xy
    return (c || EOF)
  end

  def rl_get_char=(val)
    for c in val.reverse
      _rl_unget_char(c)
    end
  end

  module_function :old_rl_get_char:rl_get_char:"rl_get_char="
end
こんな感じにRbReadline.rl_get_char()をMix-inで上書きしてやる。こうすれば、外からキー入力を差し替えられる。 RbReadline.rl_get_char = [""""""""""]
buf = Readline.readline("")
これさえ出来れば、文字入力させて最後のカーソル位置が正しい位置にいる事を確認出来る。カーソルの位置はGetConsoleScreenBufferInfo()で得られるのでWin32API使ってゴリゴリ取った。また、最終的な入力結果が文字化けしていない事を確認する為にReadConsoleOutputCharacter()も使った。
全体のコードは以下の様になった。
# encoding: CP932
require 'test/unit'
require 'rb-readline'
require 'Win32API'

module RbReadline
  @GetStdHandle = Win32API.new("kernel32","GetStdHandle",['L'],'L')
  @hConsoleHandle = @GetStdHandle.Call(STD_OUTPUT_HANDLE)
  @GetConsoleScreenBufferInfo = Win32API.new("kernel32","GetConsoleScreenBufferInfo",['L','P'],'L')
  @ReadConsoleOutputCharacter = Win32API.new("kernel32","ReadConsoleOutputCharacter",['L','P','L','L','P'],'I')

  alias :old_rl_get_char :rl_get_char
  def rl_get_char(*a)
    c = old_rl_get_char(*a)
    c.force_encoding("ASCII-8BIT"if c
    @last_xy = xy
    return (c || EOF)
  end

  def rl_get_char=(val)
    for c in val.reverse
      _rl_unget_char(c)
    end
  end

  def last_xy
    @last_xy
  end

  def last_xy=(val)
    @last_xy = val
  end

  def xy
    csbi = 0.chr * 24
    @GetConsoleScreenBufferInfo.Call(@hConsoleHandle,csbi)
    [csbi[4,2].unpack('s*').first, csbi[6,4].unpack('s*').first]
  end

  def get_line(l)
    line = 0.chr * 80
    length = 80
    coord = l << 16
    num_read = ' ' * 4
    @ReadConsoleOutputCharacter.Call(@hConsoleHandle,line,length,coord,num_read)
    line.force_encoding("Windows-31J")
  end

  module_function :old_rl_get_char:rl_get_char:"rl_get_char="
end

class TestReadline < Test::Unit::TestCase

  def setup
    Readline::HISTORY << "世界".force_encoding("ISO-8859-1")
    Readline::HISTORY << "abc".force_encoding("ISO-8859-1")
    Readline::HISTORY << "bcdef".force_encoding("ISO-8859-1")
    RbReadline.rl_get_char = []
    RbReadline.last_xy = RbReadline.xy
    puts
  end

  def test_cursor_position_normal
    a = RbReadline.xy
    buf = Readline.readline("")
    b = RbReadline.last_xy
    assert_equal 2, b[0] - a[0]
    assert_equal 0, b[1] - a[1]
  end

  def test_cursor_position_mix
    a = RbReadline.xy
    buf = Readline.readline("$$$")
    b = RbReadline.last_xy
    assert_equal 5, b[0] - a[0]
    assert_equal 0, b[1] - a[1]
  end

  def test_cursor_position_insert_single_width
    a = RbReadline.xy
    RbReadline.rl_get_char = ["a""b""c""d""e"]
    buf = Readline.readline("")
    b = RbReadline.last_xy
    assert_equal 7, b[0] - a[0]
    assert_equal 0, b[1] - a[1]
  end

  def test_cursor_position_insert_double_width
    a = RbReadline.xy
    RbReadline.rl_get_char = [""""""""""]
    buf = Readline.readline("")
    b = RbReadline.last_xy
    assert_equal 12, b[0] - a[0]
    assert_equal 0, b[1] - a[1]
  end

  def test_cursor_position_previous_history
    a = RbReadline.xy
    RbReadline.rl_get_char = ["a""\340H""\340H"]
    buf = Readline.readline("")
    b = RbReadline.last_xy
    assert_equal 5, b[0] - a[0]
    assert_equal 0, b[1] - a[1]
  end

  def test_cursor_position_next_history
    a = RbReadline.xy
    RbReadline.rl_get_char = ["a""\340H""\340H""\340P"]
    buf = Readline.readline("")
    b = RbReadline.last_xy
    assert_equal 7, b[0] - a[0]
    assert_equal 0, b[1] - a[1]
  end

  def test_cursor_position_history_include_multibyte
    a = RbReadline.xy
    RbReadline.rl_get_char = ["a""\340H""\340H""\340H"]
    buf = Readline.readline("")
    b = RbReadline.last_xy
    assert_equal 6, b[0] - a[0]
    assert_equal 0, b[1] - a[1]
  end

  def test_cursor_position_insert_into
    a = RbReadline.xy
    RbReadline.rl_get_char = ["""""""\340K""\340K""a"]
    buf = Readline.readline("")
    b = RbReadline.last_xy
    assert_equal 5, b[0] - a[0]
    assert_equal 0, b[1] - a[1]
    assert_equal "$あaいう"RbReadline.get_line(b[1])
  end
end
僕がpull requestしたpatchもtest_cursor_position_insert_intoはNG出るのでこれを直していこうと思う。
Posted at by