- 积分
- 136401
- 注册时间
- 2014-12-27
- 最后登录
- 2026-1-24
- 在线时间
- 605 小时
- 威望
- 562
- 贡献
- 29
- 金币
- 52903
- 钢镚
- 1422
- 交易凭证
- 1
- 分享
- 0
- 精华
- 33
- 帖子
- 2094
- 主题
- 1742
TA的每日心情 | 擦汗 2018-4-10 15:18 |
|---|
签到天数: 447 天 [LV.9]以坛为家II
超级版主
    
- 威望
- 562
- 贡献
- 29
- 金币
- 52903
- 钢镚
- 1422
 
|
沙发

楼主 |
发表于 2015-1-4 17:35:30
|
只看该作者
Crossbridge sample 02 Interop 分析 hellointerop.c
開啟 hellointerop.c
- #include <stdio.h>
- #include <string.h>
- #include "AS3/AS3.h"
-
- int main(int argc, char **argv)
- {
- // flascc uses GCC's inline asm syntax to allow you to write arbitrary
- // ActionScript inside your C/C++ code. This allows for very easy interop
- // between AS3 and C/C++.
- // flascc 使用 GCC 的行內組合語言語法,允許你寫任意的 ActionScript 到
- // 你的 C/C++ 代碼內。這樣可以非常容易的交互操作於 AS3 和 C/C++之間。
- //
- // For a complete guide to the asm statement and how it is used read the
- // GCC documentation: [url=http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html]http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html[/url]
- // or: [url]http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html[/url]
- // 完整的組合語言敘述句引導,並且如何使用之,請參閱 GCC 文檔:
- // [url]http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html[/url][/url] 或:
- // [url]http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html[/url][/url]
- //
- // Behind the scenes flascc turns each function/method in your C/C++ code
- // into an AS3 function. The only reason for mentioning this is that this
- // of course limits what AS3 you can put in an inline asm statement inside
- // a function. You can't write an AS3 class declaration inside an inline
- // asm statement inside a C/C++ function. You can however write a class
- // declaration outside any C/C++ function.
- // flascc 在幕後把每個 C/C++代碼的函數/方法轉成 AS3 的函數。提到這點
- // 的目的是因為,這理所當然限制了你可以放在行內組合語言敘述句的 AS3
- // 你不能在 C/C++ 函數內的行內組合語言敘述句寫 AS3 類宣告。然而你
- // 可以在任何 C/C++ 函數外面寫類宣告。
-
- // The following asm statment declares an AS3 variable and traces it out.
- // Because this asm statement doesn't mention any output parameters it must
- // be marked as volatile or GCC will optimise it away.
- // 以下的組合語言敘述句宣告一個 AS3 變數,並且 trace 之。
- // 因為此組合語言敘述句沒有提到任何標記為 volatile 變數的輸出參數,所以
- // GCC 將會優化之。
- //
- // Note: This will not be visible in the SWF console; trace() output goes to
- // the flashlog file. For more information on how to enable trace output
- // visit: [url]http://kb2.adobe.com/cps/403/kb403009.html[/url][/url]
- // 注意: 無法在 SWF 控制台看到此輸出; trace() 輸出進到 flashlog 檔案。
- // 關於如何能 trace 輸出的更多資訊,請參閱
- // [url]http://kb2.adobe.com/cps/403/kb403009.html[/url][/url]
-
- inline_as3(
- "var foo:int = 42;\n"
- "trace(\"This is inline AS3: \" + foo);\n"
- : :
- );
-
- // asm statements can take input and output parameters like this. This
- // allows for very easy interop between the two languages.
- // 組合語言敘述句能夠像此一樣獲得輸入和輸出。此允許兩個語言間很容易
- // 的交互操作。
- //
- // Not all C/C++ types have an equivalent in actionscript. They are mapped
- // as follows:
- // 並非所有的 C/C++ 類型在 actionscript 都有等價之類型。其映射如下:
- //
- // C Type | AS3 Type
- // 32bit int | int
- // 64bit int | special handling required! 需要特別處理!
- // 32bit float | Number
- // 64bit double | Number
- // pointer | int
-
- // Use AS3 to negate a 32bit int!
- // 使用 AS3 把 32 位元 int 變成負數
- int someint = 123;
- int intresult = 0;
- inline_as3(
- //指定%0為負的%1
- "%0 = -%1;\n"
- //%0是intersult
- : "=r"(intresult)
- //%1是someint
- : "r"(someint)
- );
-
- // Back in C we can take the result and print it out:
- //回到 C 獲得結果並輸出之
- printf("-%d is %d\n", someint, intresult);
-
- // Use AS3 to sqrt a double!
- // 使用 AS3 把雙精度浮點數開根號
- double somenumber = 45.0;
- double result = 0.0;
- inline_as3(
- //把%1開根號指定給%0
- "%0 = Math.sqrt(%1);\n"
- //%0 是 result,%1 是 somenumber
- : "=r"(result) : "r"(somenumber)
- );
-
- // Back in C we can take the result and print it out:
- // 回到 C 獲得結果並輸出之
- printf("sqrt of %f is %f\n", somenumber, result);
-
- // Use AS3 to manipulate a 64bit int!
- // 使用 AS3 操作 64位元 整數
- unsigned long long somelonglong = 0x243F6A8885A308D3ULL; // 64 fractional pi bits 64位元的PI小數部份
- double piapprox = 0.0;
-
- inline_as3(
- // cast to uint because %1 and %2 will be AS3 int despite "unsigned" C expression type
- // 雖然 C 表態式類型是"unsigned",還是要強制轉型成 uint,因為%1和%2是 AS3 的 int。
- "%0 = 3 + uint(%1) / 0x100000000 + uint(%2) / 0x10000000000000000\n"
- : "=r"(piapprox) : "r"((unsigned)(somelonglong >> 32)), "r"((unsigned)somelonglong));
- printf("pi approx is %.15f\n", piapprox);
-
- // Now, 64-bit out
- // 現在試試 64位元
- unsigned hi32, lo32;
- inline_as3(
- "%0 = uint(Math.sqrt(2) * 0x100000000); %1 = uint(Math.sqrt(2) * 0x10000000000000000)\n"
- : "=r"(hi32), "=r"(lo32));
- somelonglong = ((unsigned long long)hi32 << 32) | lo32;
- // 輸出根號2的52位尾數是
- printf("52 fractional bits of sqrt(2): %llx\n", somelonglong); // only 52 bit mantissa in double! 在 double 只有 52位尾數
-
- // Handling C strings involves a bit more work. Although they aren't
- // automatically converted to AS3 String objects when passed in/out of an
- // asm statement there are helper methods available.
- // 處理 C 字串要做更多事。雖然其不是自動轉成 AS3 字串物件,不過當傳遞
- // 進出於組合語言表態式時,有輔助方法。
-
- const char* words[] = {"flascc", "is", "awesome!"};
- int i;
- for(i=0; i<3; i++) {
- inline_as3(
- "trace(\"trace: \" + %0 + \": \" + CModule.readString(%1, %2));\n"
- : : "r"(i), "r"(words[i]), "r"(strlen(words[i]))
- );
- }
-
- // What about passing strings from AS3 into C/C++? Because flascc uses a
- // ByteArray for storage we need to malloc() some space in that ByteArray
- // and copy the string data in.
- // 那要如何從 AS3 傳遞字串到 C/C++呢?因為 flascc 使用一個 ByteArray
- // 來儲存,我們需要在該ByteArray malloc() 一些空間,並從中拷貝字串。
- //
- // The mallocString helper function takes an AS3 string and allocates a
- // copy in the flascc heap. This can then be freed from C using the
- // normal free() function.
- // 此 mallocString 輔助函數,要一個 AS3 字串,並且在 flascc 堆分配
- // 一個拷貝。這樣就可以在 C 使用 free() 函數釋放之。
- //
- // This example also shows that variables declared in one asm block are
- // visible to other asm blocks.
- // 此範例展示在一個組合語言區塊的變數宣告,改變數在其它組合語言區塊可見。
-
- inline_as3("var as3words = ['Interop', 'is', 'easy!'];\n");
- char* wordptrs[] = {NULL, NULL, NULL};
- for(i=0; i<3; i++) {
- inline_as3(
- "var stringptr:int = CModule.mallocString(as3words[%0]);\n"
- "CModule.write32(%1, stringptr);\n"
- : : "r"(i), "r"(&wordptrs[i])
- );
- }
-
- for(i=0; i<3; i++) {
- printf(">>> %s\n", wordptrs[i]);
- free(wordptrs[i]);
- }
- }
复制代码 名詞解釋
interop 交互操作,應該是 inter 和 operation 的結合。
volatile 變數,用於 C 的變數關鍵字,使 C 編譯器不優化該變數。
|
|