Wiki

Ticket #270: pinvoke.patch

File pinvoke.patch, 4.5 KB (added by hopscc, 14 years ago)
  • Source/Members.cobra

     
    701701 
    702702    get statements from _stmts 
    703703 
     704    get isExtern as bool 
     705        return 'extern' in _isNames 
     706 
    704707    def findLocal(name as String) as AbstractLocalVar? 
    705708        # TODO: should this use a dictionary lookup? 
    706709        for local in _locals, if local.name==name, return local 
     
    12041207        if _implementsTypeNode 
    12051208            _implementsType = _implementsTypeNode.realType 
    12061209            # 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 
    12081211            .throwError('Missing return statement for method "[_name]" which returns [_returnType.name].') 
    12091212        # check for `return` and `yield` in the same method 
    12101213        if .resultType is not .compiler.voidType 
  • Source/BackEndClr/SharpGenerator.cobra

     
    15721572        pass 
    15731573     
    15741574    def writeSharpImp(sw as CurlyWriter, skipFirst as bool) 
     1575        if .isExtern  
     1576            sw.write(';\n') 
     1577            return 
    15751578        .compiler.codeMemberStack.push(this) 
    15761579        try 
    15771580            sw.writeAndIndent('{\n') 
  • Source/CobraParser.cobra

     
    14471447        if method.statements.count 
    14481448            if 'abstract' in isNames 
    14491449                .throwError('Cannot have statements for an abstract method.') 
     1450            if 'extern' in isNames 
     1451                .throwError('Cannot have statements for a method declared extern.') 
    14501452            if not curBox.canHaveStatements 
    14511453                .throwError('Cannot have statements for a method in an [curBox.englishName].') 
    14521454        else 
    1453             if not 'abstract' in isNames and not curBox inherits Interface 
     1455            if not 'abstract' in isNames and not 'extern' in isNames and not curBox inherits Interface 
    14541456                .throwError('Missing statements. Use "pass" or other statements.') 
    14551457 
    14561458    def _endMethodDecl(didIndent as bool) as bool    
  • Tests/320-misc-two/850-pinvoke-test.cobra

     
     1#simple, silent PInvoke test 
     2use System.Runtime.InteropServices 
     3 
     4class 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 
     3use System.Runtime.InteropServices 
     4 
     5class 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"