Ticket #270: pinvoke.patch
File pinvoke.patch, 4.5 KB (added by hopscc, 14 years ago) |
---|
-
Source/Members.cobra
701 701 702 702 get statements from _stmts 703 703 704 get isExtern as bool 705 return 'extern' in _isNames 706 704 707 def findLocal(name as String) as AbstractLocalVar? 705 708 # TODO: should this use a dictionary lookup? 706 709 for local in _locals, if local.name==name, return local … … 1204 1207 if _implementsTypeNode 1205 1208 _implementsType = _implementsTypeNode.realType 1206 1209 # TODO: make sure the type is among the interfaces of the box 1207 if .compiler.errors.count==numErrors and _returnType not in [.compiler.voidType to IType, .compiler.passThroughType to IType] and not .hasReturnStmt and not .hasYieldStmt and not .hasThrowStmt and not .isAbstract and not .parentBox.isExtern and not .parentBox inherits Interface 1210 if .compiler.errors.count==numErrors and _returnType not in [.compiler.voidType to IType, .compiler.passThroughType to IType] and not .hasReturnStmt and not .hasYieldStmt and not .hasThrowStmt and not .isAbstract and not .parentBox.isExtern and not .parentBox inherits Interface and not .isExtern 1208 1211 .throwError('Missing return statement for method "[_name]" which returns [_returnType.name].') 1209 1212 # check for `return` and `yield` in the same method 1210 1213 if .resultType is not .compiler.voidType -
Source/BackEndClr/SharpGenerator.cobra
1572 1572 pass 1573 1573 1574 1574 def writeSharpImp(sw as CurlyWriter, skipFirst as bool) 1575 if .isExtern 1576 sw.write(';\n') 1577 return 1575 1578 .compiler.codeMemberStack.push(this) 1576 1579 try 1577 1580 sw.writeAndIndent('{\n') -
Source/CobraParser.cobra
1447 1447 if method.statements.count 1448 1448 if 'abstract' in isNames 1449 1449 .throwError('Cannot have statements for an abstract method.') 1450 if 'extern' in isNames 1451 .throwError('Cannot have statements for a method declared extern.') 1450 1452 if not curBox.canHaveStatements 1451 1453 .throwError('Cannot have statements for a method in an [curBox.englishName].') 1452 1454 else 1453 if not 'abstract' in isNames and not curBox inherits Interface1455 if not 'abstract' in isNames and not 'extern' in isNames and not curBox inherits Interface 1454 1456 .throwError('Missing statements. Use "pass" or other statements.') 1455 1457 1456 1458 def _endMethodDecl(didIndent as bool) as bool -
Tests/320-misc-two/850-pinvoke-test.cobra
1 #simple, silent PInvoke test 2 use System.Runtime.InteropServices 3 4 class PInvokeTst 5 6 #PInvoke entry point 7 def isCharLower(ch as char) as bool is shared, extern 8 has DllImport("user32.dll") 9 10 def main is shared 11 assert .isCharLower(c'a') 12 assert not .isCharLower(c'A') 13 print .isCharLower(c'!') -
Tests/320-misc-two/852-pinvoke-test.cobra
1 # Slightly more complicated pInvoke call/test 2 3 use System.Runtime.InteropServices 4 5 class PInvokeTst 6 7 #PInvoke entry point 8 def getUserName(lpBuffer as uint8[] has MarshalAs(UnmanagedType.LPArray), nSize as int32[] has MarshalAs(UnmanagedType.LPArray)) as bool is shared, extern 9 has DllImport("Advapi32.dll", setLastError = true, entryPoint = "GetUserName", exactSpelling=false) 10 11 def main is shared 12 userNameBytes = uint8[](256) 13 len = int[](1) 14 len[0] = 256 15 .getUserName(userNameBytes,len) 16 userName = System.Text.Encoding.ascii.getString(userNameBytes).trim(c'\0') 17 #print userName 18 #For confirmation: alternative .Net approach: 19 userCred = System.Security.Principal.WindowsIdentity.getCurrent.name.toString 20 #print userCred 21 if userCred.contains('\\') 22 userCred = userCred.split(c'\\')[1] 23 assert userCred == userName, "WinAPI call doesn't match the .Net Principal call"